Issue
Before reading / writing into csv file, when performing the command below on the original pandas dataframe
user
0 [mary, jane]
1 [alex, andrew]
When doing the command ->
df['user'].to_list()
My output looks normal and does not have the weird double-quotes.
[['mary', 'jane'], ['alex', 'andrew']]
After writing and reading into csv file, my output changed to look like the following with the weird double quotes when performing the same command, how can I maintain the original pandas dataframe without the additional double quotes in my lists when reading/writing csv files?
df = pd.read_csv('users.csv')
df['user'].to_list()
Weird Output ->
["['mary', 'jane']", "['alex', 'andrew']"]
My dataframe also changes to look like this now
user
0 ['mary', 'jane']
1 ['alex', 'andrew']
The code that I used to write and read the dataframe is as follow.
df.to_csv('users.csv')
df = pd.read_csv('users.csv')
I am not sure why is this weird change happening. Any insight is appreciated.
Thank you.
Solution
While writing into csv, following code can be used in order to remove double quotes. This parameter is an inbuilt feature of df.to_csv()
df.to_csv('users.csv',index=False,header= False,sep = ',', quoting = csv.QUOTE_NONE, escapechar = ' ')
Hope it helps !
Answered By - Arun P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.