我有一个看起来像这样的数据框
pd.DataFrame({'A': ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'],
...: 'x': [2, 2, 3, 2, 3, 1, 3],
...: 'maxValue_1': [2, 1, 2, 3, 4, 2, 1]})
Out[7]:
A x maxValue_1
0 C1 2 2
1 C2 2 1
2 C3 3 2
3 C4 2 3
4 C5 3 4
5 C6 1 2
6 C7 3 1
maxValue_2 = 2
我需要检查列'x'是否等于或大于max(df.maxValue_1,maxValue_2)
结果数据框应如下所示。
A x maxValue_1 result
0 C1 2 2 True
1 C2 2 1 True
2 C3 3 2 True
3 C4 2 3 False
4 C5 3 4 False
5 C6 1 2 False
6 C7 3 1 True
如何在不向数据帧中添加变量'maxValue_2'的情况下以高效的方式对此进行编码?
df['result'] = df['x'] >= np.maximum(df['maxValue_1'], maxValue_2)
print(df)
印刷品:
A x maxValue_1 result
0 C1 2 2 True
1 C2 2 1 True
2 C3 3 2 True
3 C4 2 3 False
4 C5 3 4 False
5 C6 1 2 False
6 C7 3 1 True
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句