下面是我的熊貓資料框,每列包含0或1。我想,以取代現有'toxic'以1如果其他列中的至少一個(severe_toxic,..., identity hate)包含1。
我嘗試了下面的代碼,但它給出了錯誤。

我試過的代碼:
# a1 - above dataframe's name
a1['toxic'] = [1 if any(a1[[severe_toxic','obscene','threat','insult','identity_hate']]) ==1]
uj5u.com熱心網友回復:
用:
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 0
Output:
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 0
Setup:
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]})
uj5u.com熱心網友回復:
使用anyPandas 而不是 Python:
cols = ['severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
a1['toxic'] = a1[cols].any(axis=1).astype(int)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333430.html
上一篇:從另一個DataFrame中查找并計算一個DataFrame的元素數量
下一篇:如何使用Pandas遍歷列?
