如何根据特定条件在数据框熊猫中的列上再添加一列

时间:2019-10-07 13:59:09

标签: python pandas

我尝试过此操作,它又创建了一个水平列,但无法获得结果

    df['Level'] = np.where((df['DIV0'] != '00') & (df['DIV1'] != '00') & (df['DIV2'] != '00') & (df['DIV3'] != '00'), 'NP', 'Full')
    df['Level'] = np.where((df['DIV0'] != '00') & (df['DIV1'] != '00') & (df['DIV2'] != '00') & (df['DIV3'] == '00'), 'NP', 'Hu')
    df['Level'] = np.where((df['DIV0'] != '00') & (df['DIV1'] != '00') & (df['DIV2'] == '00') & (df['DIV2'] == '00'), 'NP', 'Lim')
    df['Level'] = np.where((df['DIV0'] != '00') & (df['DIV1'] == '00') & (df['DIV2'] == '00') & (df['DIV2'] == '00'), 'NP', 'SEG')

是否有任何方法可以创建python函数并通过使用apply()在数据帧上应用该函数

2 个答案:

答案 0 :(得分:2)

您可以略作更改,而无需申请。

dd = {1:'SEG', 2:'Lim', 3:'Hu', 4:'Full'} #Create a dictionary mapping number of Trues to level label.
df['Level'] = (df.iloc[:, 1:] != '00').sum(axis=1).map(dd)

输出:

      Final  DIV0  DIV1  DIV2  DIV3 Level
0  78797071    78    79    70    71  Full
1  23000000    23    00    00    00   SEG
2  23450000    23    45    00    00   Lim
3  45678900    45    67    89    00    Hu

说明。

使用iloc,整数位置和切片符号,我们将位置1的所有行和列返回到末尾。然后,我们创建一个布尔矩阵以找出值不等于'00'的位置。现在,让我们在每一行上加总Trues的数量,并使用map使用字典将该值映射到正确的标签。

答案 1 :(得分:1)

这适用于此特定问题

def new_column(col0,col1,col3,col4):
    if col2 == 00:
        return 'SEG'
    elif col3 == 00:
        return 'Lim'
    elif col4 == 00:
        return 'Hu'
    else:
        return 'SEG'

df['Level'] = df[['DIV0','DIV1','DIV2','DIV3']].apply(lambda x: new_column(*x),axis=1)