Python 熊猫在没有标题的表中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29287224/
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
Pandas read in table without headers
提问by user308827
How can I read in a .csv file (with no headers) and when I only want a subset of the columns (say 4th and 7th out of a total of 20 columns), using pandas? I cannot seem to be able to do usecols
当我只想要列的子集(比如总共 20 列中的第 4 和第 7 列)时,如何使用 Pandas 读取 .csv 文件(没有标题)?我似乎无法做到usecols
采纳答案by EdChum
回答by Alex
Make sure you specify pass header=None
and add usecols=[3,6]
for the 4th and 7th columns.
确保为第 4 列和第 7 列指定 passheader=None
和 add usecols=[3,6]
。
回答by ch33hau
Previous answers were good and correct, but in my opinion, an extra names
parameter will make it perfect, and it should be the recommended way, especially when the csv has no headers
.
以前的答案是好的和正确的,但在我看来,一个额外的names
参数会使它变得完美,它应该是推荐的方式,尤其是当 csv 没有headers
.
Solution
解决方案
Use usecols
and names
parameters
用途usecols
及names
参数
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'])
Additional reading
补充阅读
or use header=None
to explicitly tells people that the csv
has no headers (anyway both lines are identical)
或用于header=None
明确告诉人们csv
没有标题(无论如何两行都是相同的)
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'], header=None)
So that you can retrieve your data by
以便您可以通过以下方式检索数据
# with `names` parameter
df['colA']
df['colB']
instead of
代替
# without `names` parameter
df[0]
df[1]
Explain
解释
Based on read_csv, when names
are passed explicitly, then header
will be behaving like None
instead of 0
, so one can skip header=None
when names
exist.
基于read_csv,当names
被显式传递时,则header
行为类似于None
而不是0
,因此可以header=None
在names
存在时跳过。