2

I have the following DataFrame:

                  Date  Year  Month  Day Security Trade  Value
0  2011-01-10 00:00:00  2011      1   10     AAPL   Buy   1500
1  2011-01-13 00:00:00  2011      1   13     AAPL  Sell   1500

When I write this to csv, an extra column is created at the start of the file:

,Date,Year,Month,Day,Security,Trade,Value
0,2011-01-10 00:00:00,2011,1,10,AAPL,Buy,1500
1,2011-01-13 00:00:00,2011,1,13,AAPL,Sell,1500

How can i resolve this?

1 Answer 1

5

The "extra column" is the index. You can suppress this by passing index=None, for example:

>>> df
    A
0  10
1  20
2  30
>>> df.to_csv("out.csv")
>>> !cat out.csv
,A
0,10
1,20
2,30
>>> df.to_csv("out.csv", index=None)
>>> !cat out.csv
A
10
20
30
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Thanks for your response, I have tried that already , and i am getting the following error.... df = pd.read_csv(input_file, index=None) TypeError: parser_f() got an unexpected keyword argument 'index'
df.to_csv isn't the same thing as pd.read_csv. df.to_csv writes the dataframe df to a csv file, pd.read_csv is for reading a csv file in. Which are you trying to do?
Sorry, my bad. The index = None, is worked. Thanks for your help
@DSM the docs state that index takes a boolean so shouldn't it be False rather than None even though that does work? I've always used False because of this
Hi, i have one more question. New to stackoverflow. and i didnt get the hang of formatting and not able to get get the question uploaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.