python每隔十帧取一张图片_Python熊猫数据帧读取from_records,“AssertionError:1列传递,传递数据有22列”...

I have a list where a is 806 in length. I want to import to a dataframe where the first item in the list is the column name. My code is:

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 columns while clearly i have only one column.

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 a and it looks fine, like a regular list. I have pasted the printed output of a and 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?

Edit (based on DSM suggestion):

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

解决方案

Instead of using from_records you want to use the default DataFrame constructor.

from_records expects 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.

b = pd.DataFrame(a[1:], columns=[a[0]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值