Created
July 26, 2017 07:57
-
-
Save chankeypathak/37f5824e0bb26d28091e017a3c7c8c6b to your computer and use it in GitHub Desktop.
if-then-else using numpy’s where()
This file contains hidden or 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
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