gpt4 book ai didi

python - 值错误 : Item wrong length 907 instead of 2000

转载 作者:行者123 更新时间:2023-12-05 03:10:54 25 4
gpt4 key购买 nike

我有一个包含 1000 列的 csv 文件。我只需要阅读前 100 列。我为此编写了这个程序:

import pandas as pd

list = []
for i in range (1, 100):
list.append(i)
df = pd.read_csv('piwik_37_2016-07-08.csv',dtype = "unicode")
df = df[df.columns.isin(list)]
df.to_csv('abc.csv', index = False)

但我得到错误:ValueError:项目错误长度 907 而不是 2000。无法弄清楚我哪里出错了

最佳答案

您的代码有很多奇怪之处。例如,没有理由为了获得数字列表而遍历范围对象并更新列表。只需使用 list(range(1,100))

但是,如果您只需要 csv 中的前 100 列,则可以使用内置功能来完成您要执行的操作:

df = pd.read_csv('piwik_37_2016-07-08.csv',dtype = "unicode", usecols = list(range(100)))

关于python - 值错误 : Item wrong length 907 instead of 2000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com