P_n
P_n

Reputation: 992

Python pandas keeps printing row numbers inside my columns

Never used pandas before and I can't find any answers for why pandas keeps adding row numbers into my output. My code:

df = pandas.read_excel(file, usecols=[2], header=1)
print(df)
with open("blah.txt", "w") as f:
    f.write(str(df))

And the output is:

   test
0  car
1  plane

Rather I would like it to be a simple as it is in the excel file and only column data in txt format:

test
car
plane

Upvotes: 0

Views: 923

Answers (1)

John Zwinck
John Zwinck

Reputation: 249404

Replace this:

with open("blah.txt", "w") as f:
    f.write(str(df))

With this:

df.to_csv("blah.txt", index=False)

If you want to control more aspects of the output: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

Upvotes: 2

Related Questions