-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFeature_Extraction_Age_Difference.py
199 lines (92 loc) · 2.75 KB
/
Feature_Extraction_Age_Difference.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
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
194
195
#!/usr/bin/env python
# coding: utf-8
# In[7]:
import pandas as pd
matches_df = pd.read_csv('final_df1.csv')
players = pd.read_csv('players_data.csv',infer_datetime_format=True,parse_dates=['dob'])
# In[35]:
matches_df
# In[12]:
players
# In[52]:
#get player id's from matches dataframe
players_df = []
for item in matches_df['player_id'].unique():
players_df.append(item)
for item in matches_df['opponent_id'].unique():
if item not in players_df:
players_df.append(item)
players_df
# In[53]:
players_concat = pd.DataFrame()
indexes = []
for player in players_df:
if len(players.loc[players['player_id']==player])!=0:
k = players.loc[players['player_id']==player].index
indexes.append(k[0])
# In[54]:
indexes
# In[55]:
len(indexes)
# In[56]:
len(players_df)
# In[64]:
players_concat = players.iloc[indexes].copy()
# In[75]:
players_concat
# In[74]:
players_concat.drop(['level_0','index','Unnamed: 0','Unnamed: 0.1','Unnamed: 0.1.1'],axis=1,inplace=True)
# In[69]:
players_concat['dob1'] = None
players_concat.reset_index(inplace=True)
# In[76]:
for i in range(0,len(players_concat)):
temp = players_concat.iloc[i]['dob']
players_concat.at[i,'dob1'] = temp[:4] +'-'+temp[4:6]
# In[89]:
players_concat
# In[79]:
#convert to datetime
players_concat['date of birth'] = pd.to_datetime(players_concat['dob1'], format='%Y-%m')
# In[81]:
players_concat['date of birth']
# In[82]:
players_concat.to_csv('players-age.csv')
# In[83]:
#now get age difference and add them to matches dataframe
matches_df = pd.read_csv('final_df1.csv',infer_datetime_format=True,parse_dates=['start_date','end_date'])
# In[84]:
matches_df['start_date']
# In[104]:
k = matches_df.iloc[9]['start_date'] - players_concat.iloc[5]['date of birth']
# In[105]:
k
# In[106]:
k.days
# In[108]:
matches_df['player age'] = None
matches_df['opponent age'] = None
for i in range(0,len(matches_df)):
player = matches_df.iloc[i]['player_id']
opponent = matches_df.iloc[i]['opponent_id']
temp1 = players_concat.loc[players_concat['player_id']==player].index
temp2 = players_concat.loc[players_concat['player_id']==opponent].index
if len(temp1) != 0:
l = matches_df.iloc[i]['start_date'] - players_concat.iloc[temp1[0]]['date of birth']
matches_df.at[i,'player age'] = l.days
if len(temp2) != 0:
p = matches_df.iloc[i]['start_date'] - players_concat.iloc[temp2[0]]['date of birth']
matches_df.at[i,'opponent age'] = p.days
player = None
opponent = None
temp1 = None
temp2 = None
l = None
p = None
print('iteration : ',i,' of ',len(matches_df))
# In[109]:
matches_df
# In[110]:
#save
matches_df.to_csv('final_df1.csv')
# In[ ]: