[英]Pandas: "ValueError: Buffer has wrong number of dimensions (expected 1, got 0)
[英]during creation of pandas DataFrame ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
我试图让 pandas 从 excel 文件中读取特定的列值,并允许我通过将所有值读入 numpy 数组来操作特定列值中的特定单个元素。
下面是我正在使用的代码
cols= 'A, AO, BB, BC'
df= pd.read_excel(path_to_excel, sheet_name=None, usecols=cols)
f1= pd.DataFrame(df, index=[0])
df1.to_numpy()
我的 df 有124412 rows X 4 columns
但是对于df1
我得到以下错误ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
。
我意识到这是一个愚蠢的问题,但任何帮助表示赞赏。
从您的代码中删除sheet_name=None
。
cols= 'A, AO, BB, BC'
df= pd.read_excel(path_to_excel, usecols=cols)
f1= pd.DataFrame(df, index=[0])
df1.to_numpy()
参数sheet_name
的默认值为Sheet1
,而不是None
。 如果您使用sheet_name=None
,熊猫会读取所有工作表,并返回一个字典,其中工作表名称为键,数据框为值。 例如:
cols= 'A, B, C'
df = pd.read_excel(filename, usecols=cols)
df
回报:
一个 | 乙 | C | |
---|---|---|---|
0 | 0 | 0 | foo1 |
1 | 1 | 1 | foo2 |
2 | 2 | 0 | foo3 |
3 | 3 | 1 | foo4 |
4 | 4 | 0 | foo5 |
5 | 0 | 0 | foo1 |
6 | 1 | 1 | foo2 |
7 | 2 | 0 | foo3 |
8 | 3 | 1 | foo4 |
9 | 4 | 0 | foo5 |
10 | 0 | 0 | foo1 |
11 | 1 | 1 | foo2 |
12 | 2 | 0 | foo3 |
13 | 3 | 1 | foo4 |
14 | 4 | 0 | foo5 |
尽管:
cols = 'A, B, C'
df = pd.read_excel(filename, sheet_name=None, usecols=cols)
回报:
{'Sheet1': A B C
0 0 0 foo1
1 1 1 foo2
2 2 0 foo3
3 3 1 foo4
4 4 0 foo5
5 0 0 foo1
6 1 1 foo2
7 2 0 foo3
8 3 1 foo4
9 4 0 foo5
10 0 0 foo1
11 1 1 foo2
12 2 0 foo3
13 3 1 foo4
14 4 0 foo5}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.