Untitled 11
Untitled 11
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid')
1:Data Loading
file_path=(r'C:\Users\Administrator\Desktop\DA-jupyter\Book1.xlsx')
data=pd.ExcelFile(file_path)
data.sheet_names
2: Data Cleaning
#converting the datetime columns for accurate time filtering
#1: gameplay_data
gameplay_data['Datetime']=pd.to_datetime(gameplay_data['Datetime'])
#2: deposit_data
deposit_data['Datetime']=pd.to_datetime(deposit_data['Datetime'])
#3: withdraw_data
withdraw_data['Datetime']=pd.to_datetime(withdraw_data['Datetime'])
unique_user2=deposit_data['User Id'].unique()
unique_user3=withdraw_data['User Id'].unique()
gameplay_data.info()
deposit_data.info()
withdraw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 355266 entries, 0 to 355265
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User ID 355266 non-null int64
1 Games Played 355266 non-null int64
2 Datetime 355266 non-null datetime64[ns]
dtypes: datetime64[ns](1), int64(2)
memory usage: 8.1 MB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 17438 entries, 0 to 17437
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User Id 17438 non-null int64
1 Datetime 17438 non-null datetime64[ns]
2 Amount 17438 non-null int64
dtypes: datetime64[ns](1), int64(2)
memory usage: 408.8 KB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3566 entries, 0 to 3565
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 User Id 3566 non-null int64
1 Datetime 3566 non-null datetime64[ns]
2 Amount 3566 non-null int64
dtypes: datetime64[ns](1), int64(2)
memory usage: 83.7 KB
At the end of each month total loyalty points are alloted to all the players. Out of which the top
50 players are provided cash benefits."
#1: gameplay_data
gameplay_data['Datetime']=pd.to_datetime(gameplay_data['Datetime'])
#2: deposit_data
deposit_data['Datetime']=pd.to_datetime(deposit_data['Datetime'])
#3: withdraw_data
withdraw_data['Datetime']=pd.to_datetime(withdraw_data['Datetime'])
october_gameplay=gameplay_data[gameplay_data['Datetime'].dt.month==10]
october_deposite=deposit_data[deposit_data['Datetime'].dt.month==10]
october_withdraw=withdraw_data[withdraw_data['Datetime'].dt.month==10]
gameplay_summery=october_gameplay.groupby('User ID').agg(
total_gameplay=('Games Played','sum')
).reset_index()
gameplay_summary=pd.merge(deposit_summary,withdraw_summary,on='User
ID',how='outer')
gameplay_summary=pd.merge(gameplay_summary,gameplay_summery,on='User
ID',how='outer').fillna(0)
gameplay_summary['Deposite_points']=0.01*gameplay_summary['total_depos
it_amount']
gameplay_summary['Withdrwal_points']=0.005*gameplay_summary['total_wit
hdraw_amount']
gameplay_summary['num_points']=0.001*gameplay_summary['num_deposits']-
gameplay_summary['num_withdraw']
gameplay_summary['gameplay_points']=0.2*gameplay_summary['total_gamepl
ay']
gameplay_summary['Loyalty_points']=(gameplay_summary['Deposite_points'
]+gameplay_summary['Withdrwal_points']+gameplay_summary['num_points']
+gameplay_summary['gameplay_points'])
gameplay_sort=gameplay_summary.sort_values(['Loyalty_points'],ascendin
g=(False))
print(gameplay_sort[['User ID','Loyalty_points']])
#Figure...
gameplay_sort_top_5=gameplay_sort.head(5)
plt.figure(figsize=(10,6))
sns.barplot(x='Loyalty_points',y='User ID',data=gameplay_sort_top_5)
plt.title('Loyalty Point Formula ')
plt.show()
User ID Loyalty_points
377 795 14649.440
196 599 10716.765
433 852 10250.183
544 972 10068.710
112 502 10049.121
.. ... ...
657 643 0.400
624 388 0.400
678 993 0.400
645 507 0.400
620 384 0.200
gameplay_data['Datetime'] = pd.to_datetime(gameplay_data['Datetime'],
errors='coerce')
withdraw_data['Datetime'] = pd.to_datetime(withdraw_data['Datetime'],
errors='coerce')
deposit_data['Datetime'] = pd.to_datetime(deposit_data['Datetime'],
errors='coerce')
def filter_data(data,date,slot):
start_time,end_time=('00:00','12:00') if slot=='S1' else
('12:00','23:59')
start_datetime=pd.to_datetime(f'{date} {start_time}')
end_datetime=pd.to_datetime(f'{date} {end_time}')
return data[(data['Datetime']>=start_datetime) &
(data['Datetime']<=end_datetime)]
target_slots = [
{"date": "2022-10-02", "slot": "S1"},
{"date": "2022-10-16", "slot": "S2"},
{"date": "2022-10-18", "slot": "S1"},
{"date": "2022-10-26", "slot": "S2"}
]
Loyalty_points=[]
gameplay_summary=pd.merge(deposit_summary,withdraw_summary,on='User
ID',how='outer')
gameplay_summary=pd.merge(gameplay_summary,gameplay_summery,on='User
ID',how='outer').fillna(0)
gameplay_summary['total_loyality_points']=(
0.01*gameplay_summary['total_deposit_amount']+
0.005*gameplay_summary['total_withdraw_amount']+
0.001*(gameplay_summary['num_deposits']-
gameplay_summary['num_withdraw']).clip(lower=0)+
0.2*gameplay_summary['total_gameplay']
)
gameplay_summary['date']=date
gameplay_summary['slot']=slot
Loyalty_points.append(gameplay_summary[['User
ID','total_loyality_points','date','slot']])
dp=pd.concat(Loyalty_points)
print(dp.head())
#figure
dp_head=dp.head(10)
plt.figure(figsize=(10,6))
sns.lineplot(x='total_loyality_points',y='User ID',data=dp_head)
plt.show()
Empty DataFrame
Columns: [User ID, total_loyality_points, date, slot]
Index: []
User ID total_loyality_points date slot
0 541 637.013 2022-10-16 S2
1 542 167.555 2022-10-16 S2
2 543 1.001 2022-10-16 S2
3 544 1490.423 2022-10-16 S2
4 546 75.002 2022-10-16 S2
User ID total_loyality_points date slot
0 541 637.013 2022-10-16 S2
1 542 167.555 2022-10-16 S2
2 543 1.001 2022-10-16 S2
3 544 1490.423 2022-10-16 S2
4 546 75.002 2022-10-16 S2
User ID total_loyality_points date slot
0 541 637.013 2022-10-16 S2
1 542 167.555 2022-10-16 S2
2 543 1.001 2022-10-16 S2
3 544 1490.423 2022-10-16 S2
4 546 75.002 2022-10-16 S2
#1: gameplay_data
gameplay_data['Datetime']=pd.to_datetime(gameplay_data['Datetime'])
#2: deposit_data
deposit_data['Datetime']=pd.to_datetime(deposit_data['Datetime'])
#3: withdraw_data
withdraw_data['Datetime']=pd.to_datetime(withdraw_data['Datetime'])
october_gameplay=gameplay_data[gameplay_data['Datetime'].dt.month==10]
october_deposite=deposit_data[deposit_data['Datetime'].dt.month==10]
october_withdraw=withdraw_data[withdraw_data['Datetime'].dt.month==10]
gameplay_summery=october_gameplay.groupby('User ID').agg(
total_gameplay=('Games Played','sum')
).reset_index()
gameplay_summary=pd.merge(deposit_summary,withdraw_summary,on='User
ID',how='outer')
gameplay_summary=pd.merge(gameplay_summary,gameplay_summery,on='User
ID',how='outer').fillna(0)
gameplay_summary['Deposite_points']=0.01*gameplay_summary['total_depos
it_amount']
gameplay_summary['Withdrwal_points']=0.005*gameplay_summary['total_wit
hdraw_amount']
gameplay_summary['num_points']=0.001*gameplay_summary['num_deposits']-
gameplay_summary['num_withdraw']
gameplay_summary['gameplay_points']=0.2*gameplay_summary['total_gamepl
ay']
gameplay_summary['Total_Loyalty_points']=(gameplay_summary['Deposite_p
oints']+gameplay_summary['Withdrwal_points']
+gameplay_summary['num_points']+gameplay_summary['gameplay_points'])
gameplay_sort=gameplay_summary.sort_values(['Total_Loyalty_points'],as
cending=(False))
print(gameplay_sort[['User ID','Total_Loyalty_points']])
User ID Total_Loyalty_points
377 795 14649.440
196 599 10716.765
433 852 10250.183
544 972 10068.710
112 502 10049.121
.. ... ...
657 643 0.400
624 388 0.400
678 993 0.400
645 507 0.400
620 384 0.200
average_deposite_amount=deposit_data['Amount'].mean()
round_avg=round(average_deposite_amount)
print(f'Average Deposite Amount is {round_avg}')
Now the company needs to determine how much bonus money should be given to the players.
Should they base it on the amount of loyalty points? Should it be based on number of games? Or
something else?
Suggest a suitable way to divide the allocated money keeping in mind the following points:
# #
october_gameplay=gameplay_data[gameplay_data['Datetime'].dt.month==10]
# #
october_deposit=deposit_data[deposit_data['Datetime'].dt.month==10]
# #
october_withdraw=withdraw_data[withdraw_data['Datetime'].dt.month==10]
# # gameplay_summery=october_gameplay.groupby('User ID').agg(
# # total_gameplay=('Games Played','sum')
# # ).reset_index()
# #
gameplay_summary=pd.merge(deposit_summary,withdraw_summary,on='User
ID',how='outer')
# #
gameplay_summary=pd.merge(gameplay_summary,gameplay_summery,how='outer
',on='User ID').fillna(0)
# #
gameplay_summary['deposite_points']=0.01*deposit_summary['total_deposi
t_amount']
# #
gameplay_summary['withdraw_points']=0.005*withdraw_summary['total_with
draw_amount']
# #
gameplay_summary['gameplay_points']=0.01*gameplay_summery['total_gamep
lay']
# #
gameplay_summary['num_points']=0.001*gameplay_summary['num_deposits']-
gameplay_summary['num_withdraw']
# #
gameplay_summary['Total_points']=(gameplay_summary['deposite_points']
+gameplay_summary['withdraw_points']
+gameplay_summary['gameplay_points']+gameplay_summary['num_points'])
# #
total_gameplay=gameplay_summary.sort_values(['Total_points'],ascending
=(False))
# # print(total_gameplay[['Total_points']].sum().round())
# # #sorting
# # gameplay_summery=october_gameplay.groupby('User ID').agg(
# # total_gameplay=('Games Played','sum')
# # ).reset_index().head(50)
# #
gameplay_summary=pd.merge(deposit_summary,withdraw_summary,on='User
ID',how='outer')
# #
gameplay_summary=pd.merge(gameplay_summary,gameplay_summery,how='outer
',on='User ID').fillna(0)
# #
gameplay_summary['deposite_points']=0.01*deposit_summary['total_deposi
t_amount']
# #
gameplay_summary['withdraw_points']=0.005*withdraw_summary['total_with
draw_amount']
# #
gameplay_summary['gameplay_points']=0.01*gameplay_summery['total_gamep
lay']
# #
gameplay_summary['num_points']=0.001*gameplay_summary['num_deposits']-
gameplay_summary['num_withdraw']
# #
gameplay_summary['sort_points']=(gameplay_summary['deposite_points']
+gameplay_summary['withdraw_points']
+gameplay_summary['gameplay_points']+gameplay_summary['num_points'])
# #
sort_gameplay=gameplay_summary.sort_values(['sort_points'],ascending=(
False))
# # print(sort_gameplay[['sort_points']].sum().round())
top_50_players['weighted_score'] = (a *
top_50_players['Total_Loyalty_points'] +
b *
top_50_players['total_gameplay'])
total_weighted_score = top_50_players['weighted_score'].sum()
top_50_players['bonus'] = (top_50_players['weighted_score'] /
total_weighted_score) * 50000
#figure
plt.figure(figsize=(10,6))
sns.lineplot(x='Total_Loyalty_points',y='bonus',data=top_50_players)
plt.title('Top 50 players accoring to the play')
plt.show()
Can you suggest any way to make the loyalty point formula more robust?"
Answers-:
Accoridng to me the loyalty point formula is fair as Deposits: Points are awarded based on 1% of
the total deposit amount. Withdrawals: Points are awarded based on 0.5% of the total
withdrawal amount. Deposit-Withdrawal Balance: Small additional points are awarded if
deposits exceed withdrawals, based on the count difference between deposits and withdrawals.
Games Played: Points are heavily weighted here, with 0.2 points awarded per game p}layed.