Python read_csv 没有正确读取此文件中的列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37553298/
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
read_csv doesn't read the column names correctly on this file?
提问by Vyraj
I have a csv file as follows:
我有一个 csv 文件,如下所示:
0 5
1 10
2 15
3 20
4 25
I want to save it as a dataframe with x,y axes as names, then plot it. However when I assign x
,y
I get a messed up DataFrame, what is happening?
我想将它保存为一个以 x,y 轴为名称的数据框,然后绘制它。但是,当我分配时x
,y
我得到了一个乱七八糟的 DataFrame,这是怎么回事?
column_names = ['x','y']
x = pd.read_csv('csv-file.csv', header = None, names = column_names)
print(x)
x y
0 0 5 NaN
1 1 10 NaN
2 2 15 NaN
3 3 20 NaN
4 4 25 NaN
I've tried without specifying None
for header
, to no avail.
我试过没有指定None
for header
,但无济于事。
回答by jezrael
Add parameter sep="\s+"
or delim_whitespace=True
to read_csv
:
添加参数sep="\s+"
ordelim_whitespace=True
到read_csv
:
import pandas as pd
temp=u"""0 5
1 10
2 15
3 20
4 25"""
#after testing replace io.StringIO(temp) to filename
column_names = ['x','y']
df = pd.read_csv(pd.compat.StringIO(temp), sep="\s+", header = None, names = column_names)
print (df)
x y
0 0 5
1 1 10
2 2 15
3 3 20
4 4 25
Or:
或者:
column_names = ['x','y']
df = pd.read_csv(pd.compat.StringIO(temp),
delim_whitespace=True,
header = None,
names = column_names)
print (df)
x y
0 0 5
1 1 10
2 2 15
3 3 20
4 4 25
回答by Chinmay Joshi
You could try this :
你可以试试这个:
import pandas as pd
column_names = ['x','y']
df = pd.read_csv('csv-file.csv',header=None)
df.columns = column_names