Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove index column while saving csv in pandas

Tags:

python

pandas

csv

I'm trying to create a csv with pandas, but when I export the data to csv it gives me an extra column

d = {'one' : pd.Series([1., 2., 3.]),'two' : pd.Series([1., 2., 3., 4.])} df0_fa = pd.DataFrame(d) df_csv = df0_fa.to_csv('revenue/data/test.csv',mode = 'w') 

Thus, my result is:

 ,one,two 0,1.0,1.0 1,2.0,2.0 2,3.0,3.0 3,4.0,4.0 

But, the expected results are:

one,two 1.0,1.0 2.0,2.0 3.0,3.0 4.0,4.0 
like image 936
JPC Avatar asked Nov 06 '14 18:11

JPC


People also ask

How do I drop a column index in pandas?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I turn off auto index in pandas?

The explanation: Dataframes always have an index, and there is no way of how to remove it, because it is a core part of every dataframe. ( iloc[0:4]['col name'] is a dataframe, too.) You can only hide it in your output.

How do I remove the index and header from a data frame?

Just simply put header=False and for eliminating the index using index=False.


1 Answers

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) 
like image 99
JD Long Avatar answered Sep 19 '22 01:09

JD Long