i have pandas dataframe where the row is [“a” “b”]
. how I can convert it into the comma separated list like [“a”, “b”]
?
I’m expecting to get back list with two comma separated elements
i have pandas dataframe where the row is [“a” “b”]
. how I can convert it into the comma separated list like [“a”, “b”]
?
I’m expecting to get back list with two comma separated elements
If your column contains string like ["a" "b"]
, you can use str.findall
to convert them as real list or str.replace
to replace ' '
by ', '
:
df = pd.DataFrame({'col1': ['["a" "b"]']})
df['col2'] = df['col1'].str.findall(r'"([^"]+)"')
df['col3'] = df['col1'].str.replace(r'"(\s+)"', '", "', regex=True)
Output:
>>> df
col1 col3 col2
0 ["a" "b"] ["a", "b"] [a, b]