python - Pandas to_csv call is prepending a comma -
i have data file, apples.csv, has headers like:
"id","str1","str2","str3","num1","num2"
i read dataframe pandas:
apples = pd.read_csv('apples.csv',delimiter=",",sep=r"\s+")
then stuff it, ignore (i have commented out, , overall issues still occurs, said stuff irrelevant here).
i save out:
apples.to_csv('bananas.csv',columns=["id","str1","str2","str3","num1","num2"])
now, looking @ bananas.csv, headers are:
,id,str1,str2,str3,num1,num2
no more quotes (which don't care about, doesn't impact in file), , leading comma. ensuing rows additional column in there, saves out 7 columns. if do:
print(len(apples.columns))
immediately prior saving, shows 6 columns...
i in java/perl/r, , less experienced python , particularly pandas, not sure if "yeah, that" or issue - have spent amusingly long trying figure out , cannot find via searching.
how can not prepending of comma, , maybe important - why doing it?
set index=false
(the default true
hence why see output) doesn't save index values csv, see docs
so this:
df = pd.dataframe({'a':np.arange(5), 'b':np.arange(5)}) df.to_csv(r'c:\data\t.csv')
results in
,a,b 0,0,0 1,1,1 2,2,2 3,3,3 4,4,4
whilst this:
df.to_csv(r'c:\data\t.csv', index=false)
results in this:
a,b 0,0 1,1 2,2 3,3 4,4
it's situation may have index values want save
Comments
Post a Comment