Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apply a function to a pandas column
# Define a function to get the name field from the first item in a dictionary list
def get_genre1(x):
x = json.loads(x)
if len(x) > 0:
return x[0]['name']
# Now use pandas.apply to use the function on one column
# In thise case create a new column called genres1 to hold the new data
movies['genre1'] = movies['genres'].apply(get_genre1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.