如何从多列中选择一个值?

问题描述 投票:0回答:1

我需要智能地组合数据框中三列的值,如下所示。代码需要选择第一个类型预测是True,只有第一个类型预测,即使另一个后续预测也是True。如果没有预测是True,则返回的值应为NaN

index    name       t1        t1_check  t2       t2_check  t3       t3_check
----------------------------------------------------------------------------
0        cow        animal    True      phone    False     fruit    False
1        apple      animal    False     fruit    True      food     True
2        carrot     vehicle   False     veg      True      animal   False
3        dog        pet       True      animal   True      object   False
4        horse      window    False     object   False     animal   True
5        car        pet       False     food     False     fruit    False

这是我试过的:

首先,我将两个相关的列组合在一起并删除了旧的列。

In:
df['t1_comb'] = str(df['t1']) + str(df['t1_check'])
df['t2_comb'] = str(df['t2']) + str(df['t2_check'])
df['t3_comb'] = str(df['t3']) + str(df['t3_check'])

df.drop(columns=['t1', 't1_check', 't2', 't2_check', 't3', 't3_check'], inplace=True)

Out:
index    name       t1_comb         t2_comb        t3_comb
---------------------------------------------------------------
0        cow        animalTrue      phoneFalse     fruitFalse
1        apple      animalFalse     fruitTrue      foodTrue
2        carrot     vehicleFalse    vegTrue        animalFalse
3        dog        petTrue         animalTrue     objectFalse
4        horse      windowFalse     objectFalse    animalTrue
5        car        petFalse        foodFalse      fruitFalse

然后我尝试用False替换包含NaN的所有条目,并从每个条目中删除True字符串。

In:
df.loc[df['t1_comb'].str.contains('False'), 't1_comb'] = np.nan
df.loc[df['t2_comb'].str.contains('False'), 't2_comb'] = np.nan
df.loc[df['t3_comb'].str.contains('False'), 't3_comb'] = np.nan

df.t1_comb = df.t1_comb.str.replace('True', '')
df.t2_comb = df.t2_comb.str.replace('True', '')
df.t3_comb = df.t3_comb.str.replace('True', '')

Out:
index    name       t1_comb         t2_comb        t3_comb
---------------------------------------------------------------
0        cow        animal          NaN            NaN
1        apple      NaN             fruit          food
2        carrot     NaN             veg            NaN
3        dog        pet             animal         NaN
4        horse      NaN             NaN            animal
5        car        NaN             NaN            NaN

下一步是我遇到一些困难,即只考虑第一个值的部分。

我需要的结果应该是这样的:

index    name       type
----------------------------
0        cow        animal
1        apple      fruit
2        carrot     veg
3        dog        pet
4        horse      animal
5        car        NaN
python data-cleaning
1个回答
2
投票

我相信更好的解决方案是可行的,但你可以为每一行使用apply

def myfunc(row):
    if row['t1_check']:
        return row['t1']
    elif row['t2_check']:
        return row['t2']
    elif row['t3_check']:
        return row['t3']
    return np.nan

df['type']=df.apply(myfunc,axis=1)
df[['name','type']]

产量

index    name       type
----------------------------
0        cow        animal
1        apple      fruit
2        carrot     veg
3        dog        pet
4        horse      animal
5        car        NaN
© www.soinside.com 2019 - 2024. All rights reserved.