伙计们,
我有这样的df:
A B C D E Yes
14 12 123 153 178 0
13 1 435 55 87 0
14 12 123 1 435 0
......
15 0 125 66 90 0
让我们说,我们有两个变量x和y,它们是整数。如果要满足以下任一条件,我想将“是”列更改为“ 1”:
df.D < x and df.E > x
df.D > x and df.E > y
df.D > y and df.E > y
Besides, I am sure df.E is always larger than df.D in those raw data.
如何快速完成?我试图以此为基础编写一些表达式,但是都存在一些问题...真的很感激。
答案 0 :(得分:1)
您可以创建一些布尔系列并将其用作pd.DataFrame.loc
.的掩码,例如:
x = 10
y = 20
m1 = (df['D'] < x) & (df['E'] > x)
m2 = (df['D'] > x) & (df['E'] > y)
m3 = (df['D'] > y) & (df['E'] > y)
df.loc[m1 | m2 | m3, 'Yes'] = 1
答案 1 :(得分:0)
除了@jpp答案外,您也可以使用np.where
df = pd.DataFrame({'A':[14,13,14,15], 'B':[12,1,12,0], 'D':[153,55,1,66],'E':[178,87,435,90],'Yes':[0,0,0,0]})
x = 100
y = 200
m1 = (df['D'] < x) & (df['E'] > x)
m2 = (df['D'] > x) & (df['E'] > y)
m3 = (df['D'] > y) & (df['E'] > y)
df['Yes'] = np.where(m1|m2|m3, 1, 0)
print(df)
输出:
A B D E Yes
0 14 12 153 178 0
1 13 1 55 87 0
2 14 12 1 435 1
3 15 0 66 90 0