Pandas read_csv get rid of double quotes

Here is my example:

I first create a dataframe and save it in a file

import pandas as pd
df = pd.DataFrame({'col_1':[['a','b','s'], 23423]})
df.to_csv(r'C:\test.csv')

Then df.col_1[0]returns a ['a','b','s']list

Later I read it from a file:

df_1 = pd.read_csv(r'C:\test.csv', quoting = 3, quotechar = '"')

Now df_1['col_1'][0]returns a "['a' 's']"string.

I want to return the list. I am experimenting with different settings read_csv, but still no luck

+4
source share
1 answer

You won’t get the list back without a little work.

Use to convert lists literal_eval

import ast

conv = dict(col_1=ast.literal_eval)
pd.read_csv(r'C:\test.csv', index_col=0, converters=conv).loc[0, 'col_1']

['a', 'b', 'c']
+6
source

Source: https://habr.com/ru/post/1676519/


All Articles