Created
February 17, 2020 20:57
-
-
Save data-enhanced/1406cc8cd1a86c8f83ffc3b016058557 to your computer and use it in GitHub Desktop.
Apply a function to a pandas column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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