Python3 & 统计固定指标的数量

场景:
有如下excel,
规则:
rValue>=17.45时,R=0,否则R=1
fValue<=17.45时,F=0,否则F=1
mValue<=17.45时,M=0,否则M=1

高价值MM: R=1,F=1,M=1
重点保持MM:R=0,F=1,M=1
重点发展MM:R=1,F=0,M=1
重点挽留MM:R=0,F=0,M=1
一般价值MM:R=1,F=1,M=0
一般保持MM:R=0,F=1,M=0
一般发展MM:R=1,F=0,M=0
潜在MM: R=0,F=0,M=0

统计以上各类型MM的数量


image.png

代码:

# -*- coding: utf-8 -*-
import pandas as pd


rfms = pd.read_excel("D:/RFM.xlsx",header=0)
rfm_li =rfms.to_dict("records")
# print(rfm_li)

avgR = 17.45
avgF = 1.54
avgM = 255.68

rfm_newlist = []
for rfm in rfm_li:
    # print(rfm)
    rfm_dict = {}
    if rfm.get('rValue') >= avgR:
        # rfm_dict['rValue'] = rfm.get('rValue')
        rfm_dict['R'] = 0
    else:
        # rfm_dict['rValue'] = rfm.get('rValue')
        rfm_dict['R'] = 1

    if rfm.get('fValue') <= avgF:
        # rfm_dict['fValue'] = rfm.get('fValue')
        rfm_dict['F'] = 0
    else:
        # rfm_dict['fValue'] = rfm.get('fValue')
        rfm_dict['F'] = 1

    if rfm.get('mValue') <= avgM:
        # rfm_dict['mValue'] = rfm.get('mValue')
        rfm_dict['M'] = 0
    else:
        # rfm_dict['mValue'] = rfm.get('mValue')
        rfm_dict['M'] = 1
    rfm_newlist.append(rfm_dict)

#print(rfm_newlist)
#print(len(rfm_newlist))

member111 = 0
member011 = 0
member101 = 0
member001 = 0
member110 = 0
member010 = 0
member100 = 0
member000 = 0

for i in range(len(rfm_newlist)):
     rfm =  dict(eval(str(rfm_newlist[i])))
     if rfm.get('R') == 1 and rfm.get('F') == 1 and rfm.get('M') == 1:
        member111 = member111 + 1
     elif rfm.get('R') == 0 and rfm.get('F') == 1 and rfm.get('M') == 1:
         member011 = member011 + 1
     elif rfm.get('R') == 1 and rfm.get('F') == 0 and rfm.get('M') == 1:
         member101 = member101 + 1
     elif rfm.get('R') == 0 and rfm.get('F') == 0 and rfm.get('M') == 1:
         member001 = member001 + 1
     elif rfm.get('R') == 1 and rfm.get('F') == 1 and rfm.get('M') == 0:
         member110 = member110 + 1
     elif rfm.get('R') == 0 and rfm.get('F') == 1 and rfm.get('M') == 0:
         member010 = member010 + 1
     elif rfm.get('R') == 1 and rfm.get('F') == 0 and rfm.get('M') == 0:
         member100 = member100 + 1
     elif rfm.get('R') == 0 and rfm.get('F') == 0 and rfm.get('M') == 0:
         member000 = member000 + 1


print("高价值MM:",member111)
print("重点保持MM:",member011)
print("重点发展MM:",member101)
print("重点挽留MM:",member001)
print("一般价值MM:",member110)
print("一般保持MM:",member010)
print("一般发展MM:",member100)
print("潜在MM:",member000)

输出结果:

高价值MM: 138
重点保持MM: 93
重点发展MM: 72
重点挽留MM: 56
一般价值MM: 107
一般保持MM: 52
一般发展MM: 578
潜在MM: 274

你可能感兴趣的:(Python3 & 统计固定指标的数量)