-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProduct_Customer.py
100 lines (84 loc) · 4.74 KB
/
Product_Customer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#-*- coding: utf-8 -*-
"""Which demographic factors
(e.g. household size, presence of children, income) appear to affect customer spend? Engagement with certain categories?
=> HH_DEMOGRAPHIC
Details : HOUSEHOLD_KEY, AGE_DESC[0], MARTIAL_STATUS_CODE[1], INCOME_DESC[2], HOMEOWNER_DESC,HH_COMP_DESC,HOUSEHOLD_SIZE_DESC[5],KID_CATEGORY_DESC[6]
1) Status
2) Age
3) HOMEOWNER_DESC -- Going to Salary
4) Household_key -->
(For each user:KidDiscription’ )
a) Dictionary which is having values KID_CATEGORY_DESC
b) Household Size
c) Income
5)
A:
45-54:
'Homeowner' :
'7' :
'KID' = 0
'Size' = 2
'Income' = 50-74K
b) Segment more/Who are married/un-married. """
from collections import OrderedDict
import csv
import yaml
import sys
customerStatus = {}
productDictOverTime = {}
def readCSV():
global customerStatus
global productDictOverTime
with open('/Users/Rick/Desktop/projectFinalFolder/segments/moreSpentOverTime.yaml', 'r' ) as f:
moreSpentOverTime = yaml.load(f)
f.close()
with open('/Users/Rick/Desktop/projectFinalFolder/segments/customerMoreQuarterProductDictOverTime.yaml', 'r') as l:
productDictOverTime = yaml.load(l)
l.close()
with open("/Users/Rick/Desktop/CSV/hh_demographic.csv", 'r') as f:
data = csv.reader(f, delimiter=',')
next(data)
for row in data:
if row[7] in moreSpentOverTime:
if row[7] in productDictOverTime:
if row[1] not in customerStatus:
customerStatus[row[1]] = {}
customerStatus[row[1]][row[0]] = {}
customerStatus[row[1]][row[0]][row[2]]={}
customerStatus[row[1]][row[0]][row[2]][row[4]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] =OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
else:
if row[0] not in customerStatus[row[1]]:
customerStatus[row[1]][row[0]] = {}
customerStatus[row[1]][row[0]][row[2]]={}
customerStatus[row[1]][row[0]][row[2]][row[4]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] =OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
else:
if row[2] not in customerStatus[row[1]][row[0]]:
customerStatus[row[1]][row[0]][row[2]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] = OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
else:
if row[4] not in customerStatus[row[1]][row[0]][row[2]]:
customerStatus[row[1]][row[0]][row[2]][row[4]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] =OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
else:
if row[7] not in customerStatus[row[1]][row[0]][row[2]][row[4]]:
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]] = {}
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] =OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
else:
if row[5] not in customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]]:
customerStatus[row[1]][row[0]][row[2]][row[4]][row[7]][row[5]] =OrderedDict(sorted(productDictOverTime.items(), key=lambda x:x[1],reverse=True))
print customerStatus
f.close()
with open("/Users/Rick/Desktop/productDictionarySorted.yaml", 'w') as f:
f.write(yaml.dump(customerStatus,default_flow_style=False))
f.close()
def main():
readCSV()
if __name__=="__main__":
main()