Pandas乘法条件产生零

ddrv8njm  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(121)

在panda中评估以下条件时,panda条件语句导致“0”,不确定结果未按要求打印的原因。

Source:
t_type  Att       Name
ABC     NaN       A1     
CCC     A_XY      NaN     
ABC     NaN       NaN         
CDE     NaN       NaN            
CDE     A_ZZ      A2
ABC     A_DD      A4

用于此操作的代码为:

conditions = [
(df['t_type'] == 'ABC') & (df['Att'].isnull()) & (df['Name'].notnull()), 
(df['t_type'] != 'ABC') & (df['Att'].notnull()) & (df['Name'].isnull()),
(df['t_type'] == 'ABC') & (df['Att'].isnull()) & (df['Name'].isnull()),
(df['t_type'] != 'ABC') & (df['Att'].isnull()) & (df['Name'].isnull())
]
values = ['Att is Null','Name is Null','ABC - Att and Name is Null','Non ABC - Att and Name is Null']

df['Remarks'] = np.select(conditions, values)

print(df.to_string())

预期输出:

t_type  Att       Name   Remarks
ABC     NaN       A1     Att is Null
CCC     A_XY      NaN    Name is Null
ABC     NaN       NaN    ABC Att and Name is Null
CDE     NaN       NaN    Non ABC Att and Name is Null
CDE     A_ZZ      A2
ABC     A_DD      A4
jckbn6z7

jckbn6z71#

Numpy.select有一个default参数,您可以将它指定为任何您想要的值:

df['Remarks'] = np.select(conditions, values, np.NaN)

print(df)

输出量:

t_type   Att Name                         Remarks
0    ABC   NaN   A1                     Att is Null
1    CCC  A_XY  NaN                    Name is Null
2    ABC   NaN  NaN      ABC - Att and Name is Null
3    CDE   NaN  NaN  Non ABC - Att and Name is Null
4    CDE  A_ZZ   A2                             nan
5    ABC  A_DD   A4                             nan

相关问题