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
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.
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.
Just simply put header=False and for eliminating the index using index=False.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With