下面是我的pandas数据框架,每一列都包含0或1。如果其他列(severe_toxic、...、identity hate)中至少有一列包含1,我将尝试用severe_toxic替换现有的identity hate。
我尝试了下面的代码,但它给出了错误。

我尝试过的代码:
# a1 - above dataframe's name
a1['toxic'] = [1 if any(a1[[severe_toxic','obscene','threat','insult','identity_hate']]) ==1]发布于 2021-10-23 12:05:44
使用:
df['toxic'] = np.where((df[df.columns[1:]]==1).any(axis=1), 1, df['toxic'])Input:
toxic severe_toxic obscene threat insult identity_hate
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 0 0 1
3 0 0 0 0 0 1
4 0 0 0 0 0 0Output:
toxic severe_toxic obscene threat insult identity_hate
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 1 0 0 0 0 1
3 1 0 0 0 0 1
4 0 0 0 0 0 0Setup:
df = pd.DataFrame(data={'toxic':[0]*5,
'severe_toxic':[0]*5,
'obscene':[0]*5,
'threat':[0]*5,
'insult':[0]*5,
'identity_hate':[0,0,1,1,0]})发布于 2021-10-23 12:12:47
使用来自熊猫的any,而不是来自Python的:
cols = ['severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
a1['toxic'] = a1[cols].any(axis=1).astype(int)https://stackoverflow.com/questions/69687938
复制相似问题