Removing Index Column in Pandas When Reading a CSV

Removing index column in pandas when reading a csv

DataFrames and Series always have an index. Although it displays alongside the column(s), it is not a column, which is why del df['index'] did not work.

If you want to replace the index with simple sequential numbers, use df.reset_index().

To get a sense for why the index is there and how it is used, see e.g. 10 minutes to Pandas.

Removing the index when appending data and rewriting CSV using pandas

Did you try

to_csv(index=False)

Remove index column while saving csv in pandas

What you are seeing is the index column. Just set index=False:

df_csv = df0_fa.to_csv('revenue/data/test.csv',mode = 'w', index=False)

Removing the index column in CSV file

In order to remove the index column while saving the csv, you can use index=False. Here is an example:

#assuming df is your dataframe
df.to_csv('Yourfile.csv',index=False)

Example:

#generating some test data
>>> from pandas import util
>>> df = util.testing.makeDataFrame()
>>> df = df.head()
>>> df
A B C D
hRACSnElRI 0.859179 -0.347567 1.750585 0.399515
icq8SOqHuD 1.978926 -0.491209 -0.674014 0.073224
WrcyQudgbD 1.321713 1.226830 1.806624 0.821147
8JeGcvp7Pw -1.033936 1.544080 -0.688740 0.800279
0pkg0966C3 -1.955992 -0.034661 -0.648781 -0.741877

#saving csv with no index column
>>> df.to_csv('test.csv',index=False)

Output in the test.csv:

A,B,C,D
0.8591789837360452,-0.34756727971713336,1.7505852695289563,0.39951524594334664
1.9789261120517005,-0.4912087678911003,-0.6740137161793854,0.07322370848561688
1.3217129370700997,1.2268304767550358,1.8066236997397453,0.8211473024218794
-1.0339361010450239,1.5440802503340652,-0.6887403441202857,0.8002786894217319
-1.955992286061844,-0.034660953939963234,-0.6487814076862116,-0.7418769734622875


Related Topics



Leave a reply



Submit