I have a pandas dataframe column which looks a little like:
Out[67]:
0 ["cheese", "milk...
1 ["yogurt", "cheese...
2 ["cheese", "cream"...
3 ["milk", "cheese"...
now, ultimately I would like this as a flat list, but in attempting to flatten this, i noticed that pandas treats ["cheese", "milk", "cream"]
as str
rather than list
How would i go about flattening this so I end up with:
["cheese", "milk", "yogurt", "cheese", "cheese"...]
[EDIT] So the answer given below appears to be:
s = pd.Series(["['cheese', 'milk']", "['yogurt', 'cheese']", "['cheese', 'cream']"])
s = s.str.strip("[]")
df = s.str.split(',', expand=True)
df = df.applymap(lambda x: x.replace("'", '').strip())
l = df.values.flatten()
print (l.tolist())
Which is great, question answered, answer accepted but it strikes me as rather inelegant solution.