对于基于给定的关于目标列的条件的多个列,求和为每个列给定的匹配数

2024-05-20 04:38:41 发布

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

我有一个数据帧,并根据目标列(data['Retention Flag']=='Retained')上的一个条件进行过滤,再次应用另一个条件,比如对每个列求和等于任何整数(这里是84)的值的数目

我有non_cat_column=['CONTRACT TERM', 'FINANCED AMOUNT', 'INTEREST RATE', 'EMI',.....etc]

我编写了一个工作正常的代码,通过条件过滤目标列,将列转换为dict,识别它们的值,以及对匹配项求和的循环。是否有其他更好的方法或容易获得的/语法

non_cat_column=['CONTRACT TERM', 'FINANCED AMOUNT', 'ACTIVATION DATE', 'CONTRACT END DATE', 'CONTRACT MATURITY DATE', 'INTEREST RATE', 'Ex Showroom', 'EMI']


for feature in non_cat_column:
    print(feature)
    print(data[feature][data['Retention Flag']=='Retained'].tail())
    print('Values of each columns which have the Retention Flag as "Retained" in dictionary format')
    print(data[feature][data['Retention Flag']=='Retained'].to_dict().values())
    print('....the total number of 84 values in the feature column where Retention Flag=Retained.....')
    print(sum(1 for value in (data[feature][data['Retention Flag']=='Retained'].to_dict().values()) if value==84))

输出:

CONTRACT TERM
13956    84
13957    60
13958    60
13959    24
13960    36
Name: CONTRACT TERM, dtype: int64
Values of each columns which have the Retention Flag as "Retained" in 
dictionary format
dict_values([84, 60, 84, 84, 60, 84, 60, 48, 60, 84, 60, 84, 60. .... etc 
])
.....the total number of 84 values in the feature column where Retention 
Flag=Retained.....  172

Tags: oftheindatacolumn条件dictfeature