if else statement with for loop in one line using Pandas in Python
import pandas as pd
df = pd.DataFrame({"Hi":['Namaste',"Hello",'Marhaba','','Namaskar','Nǐ hǎo','Hoi','','Bonjour',]})
df
Out:
Hi | |
---|---|
0 | Namaste |
1 | Hello |
2 | Marhaba |
3 | |
4 | Namaskar |
5 | Nǐ hǎo |
6 | Hoi |
7 | |
8 | Bonjour |
[0 if x =="" else 1 for x in df['Hi']]
Out:
[1, 1, 1, 0, 1, 1, 1, 0, 1]