user8498845
user8498845

Reputation:

Saving a dataframe along with metadata to a CSV file

I'm trying to write my data to text file pandas dataframe with multiple columns, but in file I see 3 column in line with "\" symbol at the end of the line and below this all line I see next columns,which I want in one line with the 1sts columns of dataframe.

For example:

 Money Honey Boney   \
1. 34 34 464
2.
.....
Yargen Newrte Ederblade
1. 34 45 545
2
...

My steps:

1. wedew= open('file.csv','w')
2. wedew.write('\n' + str(df) + '\n')
3. wedew.close()

How can I see all column in one line correctly without "\" and using my way (w\o 'to_csv')?

Upvotes: 0

Views: 803

Answers (1)

cs95
cs95

Reputation: 402814

You definitely don't want to save the string representation of a dataframe in a file, that's not what it's meant for. Use a method that is actually supposed to be used when saving to a file. Try to_csv.

df.to_csv('file.csv')

If you want to save your file as a TSV (with tabs as delimiters), you'd add a delimiter='\t' parameter inside the function call.

Upvotes: 1

Related Questions