Reputation: 119
I have the following dataFrame in pandas
df=pd.DataFrame({'questionId':[1, 2],'answer':['["Trustful", "Curious", "Nervous"]', "very good"]})
df.explode('answer')
The actual answer:
questionId answer
0 1 ["Trustful", "Curious", "Nervous"]
1 2 very good
My desired answer:
questionId answer
0 1 Trustful
0 1 Curious
0 1 Nervous
1 2 very good
Can you help me out with how can I convert
'["Trustful", "Curious", "Nervous"]' to ["Trustful", "Curious", "Nervous"]
so that I can get the answer I am looking for?
Thank you so much in advance.
Upvotes: 0
Views: 35
Reputation: 323306
Try with str.findall
s = df.answer.str.findall('"([^"]*)"')
out = df.assign(answer = np.where(s.astype(bool),s,df.answer)).explode('answer')
out
questionId answer
0 1 Trustful
0 1 Curious
0 1 Nervous
1 2 very good
Upvotes: 1