I have a dataframe in Python with 4 columns and would like to create a new column based on this Excel condition: =IF(AND(B2=B1;D2=D1;D2=14);1;0). I'm new to Python and am not sure how to start..
Should I create a for loop? If so, how to refer to the previous row?
let df['newCol']
be your new column, the below code should work ('B' and 'D' re the respective column names.
import pandas as pd
import numpy as np
df = pd.DataFrame({'B': [1,2,2,3], 'D': [2,14,14,4]})
print(df)
>>> df
B D
0 1 2
1 2 14
2 2 14
3 3 4
df['newCol']= [np.NaN]+[1 if (df.loc[i-1,'B']==df.loc[i,'B'] and df.loc[i-1,'d']==df.loc[i,'D'] and df.loc[i,'D']==14) else 0 for i in range(1,len(df))]
print(df)
>>> df
B D newCol
0 1 2 NaN
1 2 14 0.0
2 2 14 1.0
3 3 4 0.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.