By default to_csv writes a CSV like
,a,b,c
0,0.0,0.0,0.0
1,0.0,0.0,0.0
2,0.0,0.0,0.0
But I want it to write like this:
a,b,c
0,0.0,0.0,0.0
1,0.0,0.0,0.0
2,0.0,0.0,0.0
How do I achieve this? I can't set index=False
because I want to preserve the index. I just want to remove the leading comma.
df = pd.DataFrame(np.zeros((3,3)), columns = ['a','b','c'])
df.to_csv("test.csv") # this results in the first example above.
pd.read_csv("test.csv")
(where test.csv is the second example). – Trotmanpd.read_csv
interprets that implication correctly. I know this is certainly not best practice, and I don't recommend anyone do it this way, but I needed to do it this way for some legacy reasons. @Attract – Trotmanpandas
, reading this csv (per accepted answer below) in other applications/languages will result in shifted columns. I had a feeling this was an XY Problem. Your real question should have been handling the legacy reasons! I have yet to met a use case to break best practices. Good luck and happy coding! – Attract