【发布时间】:2017-01-04 05:34:11
【问题描述】:
这里是处理和保存 csv 文件的代码,以及原始输入 csv 文件和输出 csv 文件,在 Python 2.7 上使用 pandas 并想知道为什么在保存文件时开头有一个附加列?谢谢。
c_a,c_b,c_c,c_d
hello,python,pandas,0.0
hi,java,pandas,1.0
ho,c++,numpy,0.0
sample = pd.read_csv('123.csv', header=None, skiprows=1,
dtype={0:str, 1:str, 2:str, 3:float})
sample.columns = pd.Index(data=['c_a', 'c_b', 'c_c', 'c_d'])
sample['c_d'] = sample['c_d'].astype('int64')
sample.to_csv('saved.csv')
这里是保存的文件,开头多了一列,其值为0, 1, 2。
cat saved.csv
,c_a,c_b,c_c,c_d
0,hello,python,pandas,0
1,hi,java,pandas,1
2,ho,c++,numpy,0
【问题讨论】:
-
如果您不想要该列,请将
index=False设置为to_csv中的参数。 pandas.pydata.org/pandas-docs/stable/generated/… -
DataFrame的索引。请参阅here 如何禁用它。
标签: python python-2.7 csv pandas dataframe