将现有列替换为多个列的相同条件

2024-09-27 07:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的pandas数据帧,每列包含01。如果其他列(severe_toxic,…,identity hate)中至少有一列包含1,我正在尝试将现有的'toxic'替换为1

我尝试了下面的代码,但它给出了错误

Dataframe - a1

我试过的代码:

# a1 - above dataframe's name
a1['toxic'] = [1 if any(a1[[severe_toxic','obscene','threat','insult','identity_hate']]) ==1]

Tags: 数据代码namedataframepandasifa1错误
2条回答

使用Pandas而不是Python中的any

cols = ['severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
a1['toxic'] = a1[cols].any(axis=1).astype(int)

使用:

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]})

相关问题 更多 >

    热门问题