我正在尝试创建另一个基于我现有数据中多个条件的标签列
@media (max-width:1200px){
.nav-container h1 {
font-size: 1rem;
}
}
然后尝试根据以下条件在行上放置标签
df
ind group people value value_50 val_minmax
1 1 5 100 1 10
1 2 2 90 1 na
2 1 10 80 1 80
2 2 20 40 0 na
3 1 7 10 0 10
3 2 23 30 0 na
import pandas as pd
import numpy as np
df = pd.read_clipboard()
但这给我一个错误
df['label'] = np.where(np.logical_and(df.group == 2, df.value_50 == 1, df.value > 50), 1, 0)
如何在python中执行它?
答案 0 :(得分:2)
在掩膜之间使用&
:
df['label'] = np.where((df.group == 2) & (df.value_50 == 1) & (df.value > 50), 1, 0)
替代:
df['label'] = ((df.group == 2) & (df.value_50 == 1) & (df.value > 50)).astype(int)
如果将reduce
与布尔掩码列表一起使用,您的解决方案应该可以工作:
mask = np.logical_and.reduce([df.group == 2, df.value_50 == 1, df.value > 50])
df['label'] = np.where(mask, 1, 0)
#alternative
#df['label'] = mask.astype(int)