【发布时间】:2021-06-09 15:19:53
【问题描述】:
【问题讨论】:
-
那是 Excel 吗?熊猫数据框?你试过什么吗? SO中有很多答案,你搜索过吗?
【问题讨论】:
这样的事情应该可以工作,但这取决于您的数据格式。我假设它是一个熊猫数据框。
import numpy as np
df['col3'] = np.where((df['col1'] >= 4) & (df['col1'] >= 4), 'High', 'Low')
【讨论】:
尝试使用 min() 和比较:
df['col3'] = np.where(df[['col1','col2']].min(1) >=4, 'High', 'Low')
或者由于你只有两列,你可以直接比较:
df['col3'] = np.where(df['col1'].ge(4) & df['col2'].ge(4), 'High', 'Low')
为此使用 lambda 函数:
df['col3'] = df.apply(lambda row: 'High' if row['col1'] >=4 and row['col2'] >=4 else 'Low' ,axis=1)
输出:
col1 col2 col3
0 1 4 Low
1 2 5 Low
2 3 6 Low
3 4 7 High
4 5 2 Low
或者以另一种方式:
array = []
for item in df.values:
if item[0] >=4 and item[1] >=4: array.append('High')
else: array.append('Low')
df['col3'] = array
【讨论】:
def test (row):
if row['col1'] >= 4 and row['col2'] >= 4:
return 'High'
else:
return 'Low'
df['col3'] = df.apply (lambda row: test (row), axis=1)
这是来自@Tomerikoo 的建议,希望是正确的。但是@Leonardo Viotti 的回答更快。 谢谢!我现在也学习了 np.where 函数。
【讨论】: