If I have a dataframe
df = pd.DataFrame({0: "text", 1: [["foo", "bar"]]})
df
0 1
0 text [foo, bar]
And I write the df out to a tsv file like this
df.to_csv('test.tsv',sep="\t",index=False,header=None, doublequote=True)
The tsv file looks like this
text ['foo', 'bar']
How do I ensure there are double quotes around my list items and make test.tsv look like this?
text ["foo", "bar"]
Try with json.dumps
import json
df[1]=df[1].map(json.dumps)
Then
df.to_csv('test.tsv', sep="\t", index=False, header=None, doublequote=True, quoting=csv.QUOTE_NONE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With