-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_dataframe.py
More file actions
254 lines (143 loc) · 5.89 KB
/
filter_dataframe.py
File metadata and controls
254 lines (143 loc) · 5.89 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import pandas as pd
import pickle
from os.path import exists
def fil_df( df, dtype, fil_string, s):
"""
Filters based on whether it is an int, string, float, or bool
@param df: the dataframe to filter
@param dtype: the type of data being filtered
@param fill_string: the column in the dataframe to filter on
@param s: the restrictions
returns: the filtered dataframe
"""
if(dtype == "float"):
# if there is a lower bound but no upper (Watchers > 100)
if( s[0] != -1.0 and s[1] == -1.0 ):
df = df[df[fil_string] > s[0]]
# if there is a upper bound but no lower (Watchers < 100)
elif( s[0] == -1.0 and s[1] != -1.0 ):
df = df[df[fil_string] < s[1]]
# if there is a lower bound and a upper (Watchers > 100 and Watchers < 1000)
else:
df = df.loc[(df[fil_string].between(s[0], s[1]))]
if(dtype == "int"):
# if there is a lower bound but no upper (Watchers > 100)
if( s[0] != -1 and s[1] == -1 ):
df = df[df[fil_string] > s[0]]
# if there is a upper bound but no lower (Watchers < 100)
elif( s[0] == -1 and s[1] != -1 ):
df = df[df[fil_string] < s[1]]
# if there is a lower bound and a upper (Watchers > 100 and Watchers < 1000)
else:
df = df.loc[(df[fil_string].between(s[0], s[1]))]
if(dtype == "string"):
new_df = []
tmp_string = ""
tmp_fil = fil_string.replace(" ", "_")
for l in s:
tmp_string += l.replace(" ", "_")
tmp_string += "_"
tmp_string = tmp_string[:-1]
tmp_string += ".pkl"
path = "dataframes/" + tmp_fil + "_" + tmp_string
if exists(path):
df = pd.read_pickle(path)
else:
for x in range(len(original_df)):
for t in s:
if(t in str(original_df[fil_string].iloc[x]).split(",")):
new_df.append(original_df.iloc[x])
#print(original_df.iloc[x])
break
df = pd.DataFrame(new_df)
df.to_pickle(path)
if(dtype == "bool"):
df = df[df[fil_string] == True]
return df
def filter_dataframe(df, **kwargs):
"""
When a user chooses a filter on the API, this filters the dataframe being looked at.
@param df: the dataframe to filter
@param **kwargs: a dictionary with all of the potential filters
@param **kwargs keys:
(Floats)
Watchers, Commits, Branches, Releases, Contributors
Total Issues, Open Issues, Total Pull Requests, Open Pull Requests
(Ints)
Size, Forks, Stargazers
(Strings)
Languages, Main Language, Default Branch, License, Labels
(Bools)
Is Fork, Is Archived, Has Wiki
returns: the filtered dataframe
"""
# FLOATS
s = kwargs.get('Watchers')
if s != None:
df = fil_df(df, "float", "Watchers", s)
s = kwargs.get('Commits')
if s != None:
df = fil_df(df, "float", "Commits", s)
s = kwargs.get('Branches')
if s != None:
df = fil_df(df, "float", "Branches", s)
s = kwargs.get('Releases')
if s != None:
df = fil_df(df, "float", "Releases", s)
s = kwargs.get('Contributors')
if s != None:
df = fil_df(df, "float", "Contributors", s)
s = kwargs.get('Total_Issues')
if s != None:
df = fil_df(df, "float", "Total Issues", s)
s = kwargs.get('Open_Issues')
if s != None:
df = fil_df(df, "float", "Open Issues", s)
s = kwargs.get('Total_Pull_Requests')
if s != None:
df = fil_df(df, "float", "Total Pull Requests", s)
s = kwargs.get('Open_Pull_Requests')
if s != None:
df = fil_df(df, "float", "Open Pull Requests", s)
# INTS
s = kwargs.get('Size')
if s != None:
df = fil_df(df, "int", "Size", s)
s = kwargs.get('Forks')
if s != None:
df = fil_df(df, "int", "Forks", s)
s = kwargs.get('Stargazers')
if s != None:
df = fil_df(df, "int", "Stargazers", s)
# STRINGS
s = kwargs.get('Languages')
#filter dataframe by desired languages
if s != None:
df = fil_df(df, "string", "Languages", s)
s = kwargs.get('Main_Language')
#filter dataframe by desired languages
if s != None:
df = fil_df(df, "string", "Main Language", s)
s = kwargs.get('Default_Branch')
#filter dataframe by desired languages
if s != None:
df = fil_df(df, "string", "Default Branch", s)
s = kwargs.get('License')
#filter dataframe by desired languages
if s != None:
df = fil_df(df, "string", "License", s)
s = kwargs.get('Labels')
#filter dataframe by desired languages
if s != None:
df = fil_df(df, "string", "Labels", s)
# BOOLS
s = kwargs.get('Is_Fork')
if s != None:
df = fil_df(df, "bool", "Is Fork", s)
s = kwargs.get('Is_Archived')
if s != None:
df = fil_df(df, "bool", "Is Archived", s)
s = kwargs.get('Has_Wiki')
if s != None:
df = fil_df(df, "bool", "Has Wiki", s)
return df