如何增加轴:熊猫:ValueError:长度不匹配:预期轴有1个元素,新值有30个元素

时间:2018-09-07 18:18:36

标签: python pandas

如何将轴增加到30个元素?我遇到这个错误

ValueError: Length mismatch: Expected axis has 1 elements, new values have 30 elements

下面列出的是我的代码

import pandas as pd
df = pd.DataFrame()
for chunk in pd.read_csv('verybig.csv', sep='|', header=None, chunksize=1000, low_memory=False):
    df = pd.concat([df, chunk], ignore_index=True)

df.columns = ['MyTime','Attribute','Court','Energy','State','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6']

我想指出,verybig.csv约为1GB

更新:执行df.head(5)后,我看到以下内容

>>> df.head(5)
                                               0
0  2018-09-01 00:00:03,262, aid:Ut7XsnHXaqf6p4Pyc...
1  2018-09-01 00:00:16,966, aid:kzhUOAJRHxfgwKdmy...
2  2018-09-01 00:00:19,008, aid:QeKRGSs4QRP-f8dIZ...
3  2018-09-01 00:00:19,202, aid:ScGESqQOEY6YkrGoY...
4  2018-09-01 00:00:24,799, aid:Hajk1NF0tIIVELz2N...
>>>

更新2:我认为解决方案的路径是在执行以下代码后,以逗号作为分隔符拆分上一列(第0列)

    import pandas as pd
    df = pd.DataFrame()
    for chunk in pd.read_csv('verybig.csv', sep='|', header=None, chunksize=1000, low_memory=False):
        df = pd.concat([df, chunk], ignore_index=True)

1 个答案:

答案 0 :(得分:0)

如果您在df.head()中看到,则您的DataFrame只有一列。您还可以看到分隔符是','而不是'|'。您可以尝试以下操作:

df=pd.read_csv('verybig.csv', sep=',', header=None, chunksize=1000, low_memory=False)
df.columns = ['MyTime','Attribute','Court','Energy','State','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6']

对您有用吗?