Last active
October 28, 2019 21:50
-
-
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
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
# 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