Python pandas 数据帧从_records 读取,“断言错误:1 列通过,传递的数据有 22 列”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24145140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python pandas dataframe read from_records, "AssertionError: 1 columns passed, passed data had 22 columns"
提问by jason
I have a list where ais 806in length. I want to import to a dataframe where the first item in the list is the column name. My code is:
我有一个列表,其中a是806长度。我想导入到一个数据框,其中列表中的第一项是列名。我的代码是:
import pandas as pd
b = pd.DataFrame.from_records(a[1:],columns=[a[0]])
this gives me an error of AssertionError: 1 columns passed, passed data had 22 columnswhile clearly i have only one column.
这给了我一个错误,AssertionError: 1 columns passed, passed data had 22 columns而显然我只有一列。
I've tried a representation of the code and it works. So I'm not sure what is going on here. Here is a representation of the code:
我已经尝试过代码的表示并且它有效。所以我不确定这里发生了什么。这是代码的表示:
import pandas as pd
arr= ['title', 'a','b','','','']
arr= filter(None, arr)
b = pd.DataFrame.from_records(arr[1:],columns=[arr[0]] )
Must be something wrong with my list? I printed out aand it looks fine, like a regular list. I have pasted the printed output of aand placed that as the variable list, and it gives me the same error AssertionError: 1 columns passed, passed data had 22 columns. Seems like something wrong with my list. What else can I do to debug?
我的清单一定有问题吗?我打印出来a,看起来不错,就像一个常规列表。我已经粘贴了 的打印输出a并将其作为变量放置,list它给了我同样的错误AssertionError: 1 columns passed, passed data had 22 columns。我的清单似乎有问题。我还能做些什么来调试?
Edit (based on DSM suggestion):
编辑(基于 DSM 建议):
import pandas as pd
arr=['Title', '000660.ks']
b = pd.DataFrame.from_records(arr[1:],columns=[arr[0]] )
This gives AssertionError: 1 columns passed, passed data had 8 columns
这给 AssertionError: 1 columns passed, passed data had 8 columns
回答by chrisb
Instead of using from_recordsyou want to use the default DataFrame constructor.
而不是使用from_records您想使用默认的 DataFrame 构造函数。
from_recordsexpects a list of something iterable, so for example, the string '0006660.ks'is being read in as ('0','0',... ,'s')which is why you are getting an error about 8 columns in the data.
from_records期望一个可迭代的列表,例如,'0006660.ks'正在读入字符串,('0','0',... ,'s')这就是为什么您在数据中收到有关 8 列的错误的原因。
b = pd.DataFrame(a[1:], columns=[a[0]])

