Skip to content

Instantly share code, notes, and snippets.

@chankeypathak
Created July 26, 2017 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankeypathak/37f5824e0bb26d28091e017a3c7c8c6b to your computer and use it in GitHub Desktop.
Save chankeypathak/37f5824e0bb26d28091e017a3c7c8c6b to your computer and use it in GitHub Desktop.
if-then-else using numpy’s where()
import pandas as pd
df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
'''
AAA BBB CCC
0 4 10 100
1 5 20 50
2 6 30 -30
3 7 40 -50
'''
df['logic'] = np.where(df['AAA'] > 5,'high','low'); df
'''
AAA BBB CCC logic
0 4 10 100 low
1 5 20 50 low
2 6 30 -30 high
3 7 40 -50 high
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment