如何避免 Python/Pandas 在保存的 csv 中创建索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20845213/
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
How to avoid Python/Pandas creating an index in a saved csv?
提问by Alexis
I am trying to save a csv to a folder after making some edits to the file.
在对文件进行一些编辑后,我试图将 csv 保存到文件夹中。
Every time I use pd.to_csv('C:/Path of file.csv')the csv file has a separate column of indexes. I want to avoid printing the index to csv.
每次我使用pd.to_csv('C:/Path of file.csv')csv 文件时都有一个单独的索引列。我想避免将索引打印到 csv。
I tried:
我试过:
pd.read_csv('C:/Path to file to edit.csv', index_col = False)
And to save the file...
并保存文件...
pd.to_csv('C:/Path to save edited file.csv', index_col = False)
However, I still got the unwanted index column. How can I avoid this when I save my files?
但是,我仍然得到了不需要的索引列。保存文件时如何避免这种情况?
回答by Probably rgbkrk
Use index=False.
使用index=False.
df.to_csv('your.csv', index=False)
回答by blitu12345
There are two ways to handle the situation where we do not want the index to be stored in csv file.
有两种方法可以处理我们不希望将索引存储在 csv 文件中的情况。
As others have stated you can use index=Falsewhile saving your
dataframe to csv file.df.to_csv('file_name.csv',index=False)- Or you can save your dataframe as it is with an index, and while reading you just drop the column unnamed 0containing your previous index.Simple!
df.to_csv(' file_name.csv ')df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)
正如其他人所说,您可以在将 数据帧保存到 csv 文件时使用index=False
。df.to_csv('file_name.csv',index=False)- 或者,您可以使用索引保存您的数据框,并在阅读时删除包含您之前索引的未命名列0。简单!
df.to_csv(' file_name.csv ')df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)
回答by khaled salah
Another solution if you want to keep this column as index.
如果您想将此列保留为索引,则另一种解决方案。
pd.read_csv('filename.csv', index_col='Unnamed: 0')
回答by Iván Rodríguez
If you want a good format the next statement is the best:
如果你想要一个好的格式,下一个语句是最好的:
dataframe_prediction.to_csv('filename.csv', sep=',', encoding='utf-8', index=False)
In this case you have got a csv file with ',' as separate between columns and utf-8 format. In addition, numerical index won't appear.
在这种情况下,您有一个带有 ',' 的 csv 文件,在列和 utf-8 格式之间是分开的。此外,不会出现数字索引。
回答by Lucas P.
As others have stated, if you don't want to save the index column in the first place, you can use df.to_csv('processed.csv', index=False)
正如其他人所说,如果您不想首先保存索引列,则可以使用 df.to_csv('processed.csv', index=False)
However, since the data you will usually use, have some sort of index themselves, let's say a 'timestamp' column, I would keep the index and load the data using it.
但是,由于您通常会使用的数据本身具有某种索引,例如“时间戳”列,我会保留索引并使用它加载数据。
So, to save the indexed data, first set their index and then save the DataFrame:
因此,要保存索引数据,首先设置它们的索引,然后保存 DataFrame:
df.set_index('timestamp')
df.to_csv('processed.csv')
Afterwards, you can either read the data with the index:
之后,您可以使用索引读取数据:
pd.read_csv('processed.csv', index_col='timestamp')
or read the data, and then set the index:
或者读取数据,然后设置索引:
pd.read_csv('filename.csv')
pd.set_index('column_name')
回答by amalik2205
If you want no index, read file using:
如果不需要索引,请使用以下命令读取文件:
import pandas as pd
df = pd.read_csv('file.csv', index_col=0)
save it using
使用保存
df.to_csv('file.csv', index=False)

