2

I have a column in my Dataframe that has data in the below format:

id, value
10001, "[{'self': 'https://www.weburl.com', 'value': 'Value 1', 'id': '101'}, 
   {'self': 'https://www.weburl.com', 'value': 'Value 2', 'id': '102'}]"
10002, "[{'self': 'https://www.weburl.com', 'value': 'Value 1', 'id': '101'}, 
   {'self': 'https://www.weburl.com', 'value': 'Value 2', 'id': '102'}, 
   {'self': 'https://www.weburl.com', 'value': 'Value 4', 'id': '104'}]"

I am trying to extract data such that I get the below output (extract all values corresponding to value field) as a new column:

id, new_value
10001, Value 1, Value 2
10002, Value 1, Value 2, Value 4

1 Answer 1

3

If your values are valid json, you could use ast.literal_eval with list comprehension:

df["value"] = df["value"].apply(literal_eval)
df["value"] = [", ".join(k["value"] for k in i) for i in df["value"]]

print (df)

      id                      value
0  10001           Value 1, Value 2
1  10002  Value 1, Value 2, Value 4
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it worked well. Just trying to figure a way out to handle null in between...

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.