pandas to csv no index
pandas write csv without index.pandas to csv without index.
read_csv
– Load a CSV file into a DataFrame.to_csv
– Write object to a comma-separated values (csv) file.
import pandas as pd df = pd.read_csv('file.csv', index_col=0)
pandas save csv without index
- You can use
index=False
while saving your dataframe to csv file. - You can save your dataframe as it is with an index, and while reading you just drop the column unnamed 0 containing your previous index
df.to_csv(' file_name.csv ') df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)
Save the CSV File Using
df.to_csv('file.csv', index=False)
to_csv
df = pd.DataFrame({'name': ['Raphael', 'Donatello'], 'mask': ['red', 'purple'], 'weapon': ['sai', 'bo staff']}) df.to_csv(index=False)
If you want to keep this column as index.
pd.read_csv('filename.csv', index_col='Unnamed: 0')
If you want a good format the next statement is the best
df.to_csv('filename.csv', sep=',', encoding='utf-8', index=False)