Skip to content

Instantly share code, notes, and snippets.

@data-enhanced
Created February 17, 2020 20:57
Show Gist options
  • Save data-enhanced/1406cc8cd1a86c8f83ffc3b016058557 to your computer and use it in GitHub Desktop.
Save data-enhanced/1406cc8cd1a86c8f83ffc3b016058557 to your computer and use it in GitHub Desktop.
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