JPC JPC - 1 month ago 7
Python Question

pandas to_csv first extra column remove, how to?

I'm trying to create csv with pandas , but when I export to csv it gave me one extra row

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')


so my result would be :

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


But what I want is

one,two
1.0,1.0
2.0,2.0
3.0,3.0
,4.0

Answer

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)