-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions_creator.py
More file actions
193 lines (166 loc) · 9.1 KB
/
transactions_creator.py
File metadata and controls
193 lines (166 loc) · 9.1 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 10:42:00 2024
@author: kerim
"""
import pandas as pd
import numpy as np
import random
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
# Müşteri verilerini içeren CSV dosyasını okuma
customers_df = pd.read_csv('musteriler_guncel.csv')
# Boş bir transactions DataFrame'i oluşturma
transactions_data = []
# Her müşteri için işlem oluşturma
for index, customer in customers_df.iterrows():
customer_id = customer['CLIENTNUM']
income_level = customer['Income_Category']
total_transactions = customer['Total_Trans_Ct']
credit_limit = customer['Credit_Limit']
occupation = customer['jobs']
# Her müşteri için maaş işlemi ekleme
if occupation != 'unemployed':
if income_level == 'Unknown':
if credit_limit <= 5000:
annual_income = random.uniform(20000, 30000)
elif credit_limit <= 10000:
annual_income = random.uniform(30000, 50000)
elif credit_limit <= 20000:
annual_income = random.uniform(50000, 70000)
else:
annual_income = random.uniform(70000, 100000)
elif income_level == 'Less than $40K':
annual_income = random.uniform(20000, 40000)
elif income_level == '$40K - $60K':
annual_income = random.uniform(40000, 60000)
elif income_level == '$60K - $80K':
annual_income = random.uniform(60000, 80000)
elif income_level == '$80K - $120K':
annual_income = random.uniform(80000, 120000)
elif income_level == '$120K +':
annual_income = random.uniform(120000, 200000)
else:
annual_income = random.uniform(20000, 100000) # Default case
# Aylık maaş hesaplaması ve 50'ye tam bölünen en yakın sayıya yuvarlama
monthly_income = round(annual_income / 12 / 50) * 50
# Son 6 ay için işlemleri ekleme
current_date = datetime.now()
rent_payment = random.random() < 0.65 # %65 olasılıkla kira ödemesi
for month in range(6):
transaction_date = current_date - relativedelta(months=month)
transaction_date = transaction_date.replace(day=random.randint(1, 5)) # Ayın 1'i ile 5'i arasında
# Maaş işlemi ekleme
transactions_data.append({
'TransactionID': len(transactions_data) + 1,
'CustomerID': customer_id,
'TransactionType': 'IncomingTransfer',
'Amount': monthly_income,
'TransactionDate': transaction_date,
'Description': 'Salary',
})
# Kredi kartı ödemesi (maaş tarihinden 5 gün sonra)
total_revolving_bal = customer['Total_Revolving_Bal']
if total_revolving_bal > 0:
payment_ratio = random.uniform(1, 1.5)
payment_amount = round(total_revolving_bal * payment_ratio, -2)
credit_card_payment_date = transaction_date + timedelta(days=5)
transactions_data.append({
'TransactionID': len(transactions_data) + 1,
'CustomerID': customer_id,
'TransactionType': 'Payment',
'Amount': payment_amount,
'TransactionDate': credit_card_payment_date,
'Description': 'Credit Card Payment',
})
# Kira veya diğer ödemeler ekleme
transaction_date = current_date - relativedelta(months=month)
transaction_date = transaction_date.replace(day=random.randint(10, 18)) # Ayın 10'u ile 18'i arasında
if rent_payment:
rent_amount = round(monthly_income * random.uniform(0.25, 0.65), -2)
transactions_data.append({
'TransactionID': len(transactions_data) + 1,
'CustomerID': customer_id,
'TransactionType': 'Payment',
'Amount': rent_amount,
'TransactionDate': transaction_date,
'Description': 'Rent Payment',
})
else:
other_expense_amount = round(monthly_income * random.uniform(0.25, 0.45), -2)
transactions_data.append({
'TransactionID': len(transactions_data) + 1,
'CustomerID': customer_id,
'TransactionType': 'Payment',
'Amount': other_expense_amount,
'TransactionDate': transaction_date,
'Description': random.choice(['Insurance Payment', 'Installment Payment', 'Subscription Payment']),
})
# Müşteriye ait işlem sayısı olarak Total_Trans_Ct sütununu kullanma
num_transactions = int(total_transactions)
# İşlemler için boş bir liste
transaction_amounts = []
for i in range(num_transactions):
# Rastgele işlem türü seçimi
transaction_type = random.choices(['Purchase', 'Withdrawal', 'OutgoingTransfer', 'Payment', 'Deposit', 'OnlineShopping', 'Return', 'IncomingTransfer'],
weights=[0.2, 0.15, 0.1, 0.1, 0.2, 0.1, 0.1, 0.05], k=1)[0]
# Rastgele işlem miktarı seçimi (müşteri gelir seviyesine göre)
if income_level == 'Unknown':
amount = round(random.uniform(10, 500), 2)
elif income_level == 'Less than $40K':
amount = round(random.uniform(10, 250), 2)
elif income_level == '$40K - $60K':
amount = round(random.uniform(20, 400), 2)
elif income_level == '$60K - $80K':
amount = round(random.uniform(50, 550), 2)
elif income_level == '$80K - $120K':
amount = round(random.uniform(100, 750), 2)
elif income_level == '$120K +':
amount = round(random.uniform(200, 950), 2)
else:
amount = round(random.uniform(10, 500), 2) # Default case
transaction_amounts.append(amount)
# Rastgele işlem tarihi seçimi (son 6 ay içinde)
start_date = current_date - timedelta(days=180)
transaction_date = start_date + timedelta(days=random.randint(1, 180))
# İşlem saatini rastgele seçelim (haftaiçi, haftasonu ve gündüz/gece ayarlaması)
if transaction_date.weekday() < 5: # Haftaiçi ise
if random.random() < 0.6: # Gündüz saatleri için
transaction_date = transaction_date.replace(hour=random.randint(9, 17))
else: # Gece saatleri için
transaction_date = transaction_date.replace(hour=random.randint(18, 23))
else: # Haftasonu ise
transaction_date = transaction_date.replace(hour=random.randint(10, 22))
# İşlem açıklaması oluşturma
if transaction_type == 'Purchase':
description = random.choice(['Grocery', 'Clothing', 'Electronics', 'Restaurant', 'Travel', 'Pharmacy', 'Gas', 'Furniture', 'Home Improvement', 'Entertainment'])
elif transaction_type == 'Withdrawal':
description = random.choice(['ATM Withdrawal', 'Branch Withdrawal', 'Mobile App Withdrawal'])
elif transaction_type == 'Payment':
description = random.choice(['Bill Payment', 'Loan Payment', 'Mortgage Payment'])
elif transaction_type == 'Deposit':
description = random.choice(['Cash Deposit', 'Check Deposit', 'Direct Deposit', 'Mobile Deposit'])
elif transaction_type == 'OnlineShopping':
description = random.choice(['Amazon', 'eBay', 'AliExpress', 'Etsy', 'Walmart', 'Best Buy', 'Target'])
elif transaction_type == 'Return':
description = random.choice(['Amazon Return', 'eBay Return', 'AliExpress Return', 'Etsy Return', 'Walmart Return', 'Best Buy Return', 'Target Return', 'Store Return'])
elif transaction_type == 'IncomingTransfer':
description = 'Freelance Payment' # Değiştirilebilir
else:
description = random.choice(['Bank Transfer', 'Peer-to-Peer Transfer', 'Wire Transfer', 'International Transfer'])
# İşlem bilgilerini transactions_data listesine ekleme
transactions_data.append({
'TransactionID': len(transactions_data) + 1,
'CustomerID': customer_id,
'TransactionType': transaction_type,
'Amount': amount,
'TransactionDate': transaction_date,
'Description': description,
})
# transactions_data listesini DataFrame'e çevirme
transactions = pd.DataFrame(transactions_data)
# TransactionDate sütununun format ayarı
transactions['TransactionDate'] = pd.to_datetime(transactions['TransactionDate'])
# DataFrame'i CSV dosyasına kaydetme
transactions.to_csv('transactions.csv', index=False)
print("transactions.csv dosyası başarıyla oluşturuldu.")