我尝试过此操作,它又创建了一个水平列,但无法实现结果
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()在数据框上应用该函数
您可以在不应用的情况下进行一些其他操作。
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使用字典将该值映射到正确的标签。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句