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
Concatenate a List of Pandas Dataframes Together
Get Raw Post Body in Python Flask Regardless of Content-Type Header
Python Datetime Object Show Wrong Timezone Offset
What Can Multiprocessing and Dill Do Together
Forced Naming of Parameters in Python
File Not Found Error When Launching a Subprocess Containing Piped Commands
Importerror: No Module Named Crypto.Cipher
How to Extract Top-Level Domain Name (Tld) from Url
Matplotlib Savefig() Plots Different from Show()
How to Convert Strings in a Pandas Data Frame to a 'Date' Data Type
How to Run Pygame or Pyglet in a Browser
How to Terminate a Thread When Main Program Ends
How to Convert a File to Utf-8 in Python
E731 Do Not Assign a Lambda Expression, Use a Def
How to Add Items into a Numpy Array
Get Last "Column" After .Str.Split() Operation on Column in Pandas Dataframe
How to Use Pip to Install a Package from a Private Github Repository