0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.