I imported CSV using Pandas and one column was read with string rows. Studying the entries for this series (column), I see that they should be lists. For instance:
df['A'] = pd.Series(['["entry11"]', '["entry21","entry22"]', '["entry31","entry32"]'])
I would like to extract list items from strings. So far I have tried the following chain:
df['A'] = df['A'].replace("'",'',regex=True).
replace('\[','',regex=True).
replace('\]','',regex=True).
str.split(",")
(all on one line, of course).
and this returns me the list items in one column.
- ['"entry11"']
- ['"entry21", "entry22"']
- ['"entry31", "entry32"']
My question is: is there a more efficient way to do this? It seems very stressful for something that should be a little easier.
source
share