Skip to content

Instantly share code, notes, and snippets.

@pierdom
Last active October 28, 2019 21:50
Show Gist options
  • Save pierdom/cab3e44641442cf2c69dc1ead59df0ff to your computer and use it in GitHub Desktop.
Save pierdom/cab3e44641442cf2c69dc1ead59df0ff to your computer and use it in GitHub Desktop.
[Conditionally update Pandas DataFrame column] It is equivalent to SQL: UPDATE table SET column_to_update = 'value' WHERE condition #python #pandas #datascience
# set 'column_to_update' to 0 when 'condition_column' is 0
df.loc[df['condition_column'] == 0, 'column_to_update'] = 0
# or with a if-then-else scheme
df["mycol"] = np.where((df.mycol2=="test"), 0, 1)
# condition then else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment