Reputation: 745
I have this Pandas DataFrame with two columns label
and time
>>> df = pd.DataFrame([{'a':{'tier':'one','app':'frontend'},'time':100}])
>>> df
a time
0 {u'tier': u'one', u'app': u'frontend'} 100
Column label
stores a dict.
When I print the dataframe, I get the expected row values
>>> print(df.to_csv(index=False,header=False,sep='|'))
{'tier': 'one', 'app': 'frontend'}|100
I wanted to convert these row JSON values to a string and hence I did
>>> df['a'] = df['a'].apply(lambda x: json.dumps(x))
>>> df
a time
0 {"tier": "one", "app": "frontend"} 100
But with df.to_csv(), I get this issue where I get two times double quotes
>>> print(df.to_csv(index=False,header=False,sep='|'))
"{""tier"": ""one"", ""app"": ""frontend""}"|100
When the expected output should be
{"tier": "one", "app": "frontend"}|100
This behaviour seems very unusual. Am I going wrong somewhere here ?
Upvotes: 3
Views: 888
Reputation: 820
Use quoting=csv.QUOTE_NONE in to_csv function, as:
import csv
print(df.to_csv(index=False,header=False,sep='|',quoting=csv.QUOTE_NONE))
For more details, read the docs.
Upvotes: 3