1

I have a dataframe like so:

Store matches
Murphy's [{'domain': 'murphyscolumbus.com', 'location': 'Columbus, OH'}, {'domain': 'murphystampa.com', 'location': 'Tampa, FL'}]
Bill's [{'domain': 'billsdallas.com', 'location': 'Dallas, TX'}, {'domain': 'billsorlando.com', 'location': 'Orlando, FL'}]

What I want is a dataframe like so:

Store domains
Murphy's ['murphyscolumbus.com', 'murphystampa.com']
Bill's ['billsdallas.com','billsorlando.com']

I'm hoping for something less computationally expensive than something like a for loop that steps through as the dataframe is quite large.

1 Answer 1

1

Try:

from ast import literal_eval

# apply literal_eval if necessary
df["matches"] = df["matches"].apply(literal_eval)

df["domains"] = df.pop("matches").apply(lambda x: [d["domain"] for d in x])
print(df)

Prints:

      Store                                  domains
0  Murphy's  [murphyscolumbus.com, murphystampa.com]
1    Bill's      [billsdallas.com, billsorlando.com]
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.