pandas ValueError:项目错误长度 907 而不是 2000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38287400/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:32:57  来源:igfitidea点击:

ValueError: Item wrong length 907 instead of 2000

pythonpython-2.7pandas

提问by Diganta Bharali

I have a csv file, that has 1000 columns. I need to read only the first 100 columns. I wrote this program for that:

我有一个 csv 文件,有 1000 列。我只需要阅读前 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)

But I get error: ValueError: Item wrong length 907 instead of 2000. Can't figure out where I went wrong

但我收到错误:ValueError: Item wrong length 907 而不是 2000。不知道我哪里出错了

回答by juanpa.arrivillaga

There are a lot of things strange about your code. For example, there is no reason to iterate over the range object and update a list just to get a list of numbers. Just use list(range(1,100)).

你的代码有很多奇怪的地方。例如,没有理由迭代范围对象并更新列表只是为了获取数字列表。只需使用list(range(1,100)).

However, if you just need the first 100 columns in the csv, there is built-in functionality for what you are trying to do:

但是,如果您只需要 csv 中的前 100 列,则有内置功能可用于您尝试执行的操作:

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

回答by Shahidur

Though its down voted, still answering as its a small correction in the code.

虽然它被否决了,但仍然作为代码中的一个小更正来回答。

import pandas as pd

l = list(range(0,100))
df = pd.read_csv('piwik_37_2016-07-08.csv',dtype = "unicode")
df = df.loc[:,df.columns.isin(l)]
df.to_csv('abc.csv', index = False)