-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
217 lines (155 loc) · 7.26 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from flask import Flask
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import pymongo
from pymongo import MongoClient
def fun(f, i2):
if i2 == 'b':
return f / 10
elif i2 == 'c':
return f / 1000
else:
g = f / 1000
if (g > 1):
return g
else:
return f
app = Flask(__name__)
sell = 'aw'
@app.route('/')
def hello_world():
ten_days = datetime.today() - timedelta(10)
# # Connection à la base de donnés Mongo
# In[166]:
client = pymongo.MongoClient(
"mongodb+srv://amine:testtest@cluster0.qlxh6.mongodb.net/test2?retryWrites=true&w=majority")
db = client.test2
collection = db['Ouedkniss-today']
collection_sell = db['Sell']
collection_entropot = db['Entropot']
global sell
sell = pd.DataFrame(list(collection_sell.find({}, {'_id': False})))
sell.set_index('id', inplace=True)
entopot = pd.DataFrame(list(collection_entropot.find({}, {'_id': False})))
entopot.set_index('id', inplace=True)
# # Lire la base de donnés comme DataFrame
# In[167]:
file = pd.DataFrame(list(collection.find({}, {'_id': False})))
# # Lire les fichiers sell (voiture vendu) et entrpot (tout les voitures)
# In[168]:
# # Définir l'index
# In[169]:
file = file[~file.loc[:, 'id'].isna()]
file.loc[:, 'id'] = file.id.astype('int')
file.set_index('id', inplace=True)
file = file[~file.model.isna()]
# # Définir la date et les voitures affichés dans les 10 derniers jours
# In[170]:
file.date_annonce = file.date_annonce.str.split().str.get(0)
file.date_annonce = pd.to_datetime(file.date_annonce, dayfirst=True)
file = file[file.date_annonce > ten_days]
# In[171]:
sell2 = sell.reset_index().merge(file.reset_index().drop_duplicates(), on=['id'],
how='left', indicator=True)
sell2 = sell2[sell2._merge == 'left_only'].iloc[:, 0:21]
sell2.set_index('id', inplace=True)
sell2.columns = sell.columns
# # Split Model en trois column
# In[172]:
file.model = file.model.str.split().str.get(0)
file.proDate = file.proDate.str.split().str.get(-1)
file.brand = file.brand.str.split().str.get(1)
file.notes = file.notes.str.split(n=3).str[2:-1].str.join(sep=' ')
file.loc[:, 'proDate'] = file.loc[:, 'proDate'].astype('int')
# # Split Moteur en trois column
# In[173]:
file.loc[:, 'ch'] = file.moteur.str.split().str.get(-1)
file.loc[:, 'tdi'] = file.moteur.str.split().str.get(1)
file.loc[:, 'litre'] = file.moteur.str.split().str.get(0)
file.loc[:, 'notes_moteur'] = file.moteur.str.split(n=3).str[2:-1].str.join(sep=' ')
file.drop(columns='moteur', inplace=True)
# # Traiter le litrage du moteur exp 1.6
# In[174]:
file.loc[:, 'litre'] = file.loc[:, 'litre'].str.replace('[^\w]', '.', regex=True)
file.loc[:, 'litre'] = file.loc[:, 'litre'].str.extract(r'(\d\W\d)').iloc[:, 0]
file.loc[:, 'litre'] = file.litre.astype('float')
# # Traiter le prix
# In[175]:
file.loc[:, 'price'] = file.loc[:, 'price'].str.extract(r'(\d\d\d*)').iloc[:, 0]
file = file[~file.price.isna()]
file.loc[:, 'price'] = file.loc[:, 'price'].astype('int')
file.loc[(file.price > 600) & (file.litre < 1.6), 'price'] = (
file[(file.price > 600) & (file.litre < 1.6)].price / 10).astype('int')
# # Traiter le ch
# In[176]:
file.loc[:, 'ch'] = file.loc[:, 'ch'].str.extract(r'(\d\d\d*)').iloc[:, 0]
file.loc[:, 'ch'] = file.loc[:, 'ch'].fillna(170)
file.loc[:, 'ch'] = file.loc[:, 'ch'].astype('int')
# # Traiter les Wilayas
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Ain temouchent', 'AinTemouchent')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Ain defla', 'AinDefla')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('El taref', 'ElTaref')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('El oued', 'ElOued')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('El bayadh', 'ElBayadh')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Bordj bou arreridj', 'BordjBouArreridj')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Souk ahras', 'SoukAhras')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Oum el bouaghi', 'OumElBouaghi')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Sidi bel abbes', 'SidiBelAbbes')
file.loc[:, 'location'] = file.loc[:, 'location'].str.replace('Tizi ouzou', 'TiziOuzou')
file.loc[:, 'location_wilaya'] = file.loc[:, 'location'].str.split().str.get(0)
file.loc[:, 'location_ville'] = file.loc[:, 'location'].str.split(n=3).str[1:].str.join(sep=' ')
file = file.drop('location', axis=1)
file = file.reindex(columns=sell.columns)
# # Traiter le kilométrage
file.loc[:, 'kilometrage'] = file.loc[:, 'kilometrage'].str.replace(' km', '')
file.loc[:, 'kilometrage'] = file.loc[:, 'kilometrage'].astype('int')
file.loc[:, 'kilometrage'] = file.loc[:, 'kilometrage'].apply(fun, i2='a')
file.loc[((file.loc[:, 'proDate'] > 2010) & (file.loc[:, 'kilometrage'] > 600)), 'kilometrage'] = file.loc[
((file.loc[:, 'proDate'] > 2010) & (file.loc[:, 'kilometrage'] > 600)), 'kilometrage'].apply(fun, i2='b')
file.loc[((file.loc[:, 'proDate'] > 2020) & (file.loc[:, 'kilometrage'] < 1000)), 'kilometrage'] = file.loc[
((file.loc[:, 'proDate'] > 2020) & (file.loc[:, 'kilometrage'] < 1000)), 'kilometrage'].apply(fun, i2="c")
# In[179]:
entopot.date_annonce = pd.to_datetime(entopot.date_annonce, unit='ms')
df_all = entopot.loc[entopot.date_annonce > ten_days].merge(file.reset_index(), on=['id'],
how='left', indicator=True)
# In[180]:
sell = df_all[df_all.loc[:, '_merge'] == 'left_only'].iloc[:, 0:21]
sell.set_index('id', inplace=True)
sell.columns = entopot.columns
# In[181]:
df_all2 = entopot.reset_index().merge(file.reset_index().drop_duplicates(), on=['id'],
how='right', indicator=True)
# In[182]:
new_cars = df_all2[df_all2.loc[:, '_merge'] == 'right_only'].iloc[:, np.r_[0, 21:41]]
new_cars.set_index('id', inplace=True)
new_cars.columns = entopot.columns
# In[183]:
entropot = pd.concat([entopot, new_cars])
entropot.reset_index(inplace=True)
entropot.drop_duplicates('id', inplace=True)
collection_entropot.drop()
data_dict = entropot.drop_duplicates('id').to_dict("records")
collection_entropot.insert_many(data_dict)
# In[184]:
selll = pd.concat([sell, sell2])
selll.reset_index(inplace=True)
collection_sell.drop()
data_dict = selll.drop_duplicates('id').to_dict("records")
collection_sell.insert_many(data_dict)
# In[191]:
return 'Hello World!'
def brand(m):
global sell
if m in (sell.brand.value_counts()[sell.brand.value_counts()>5]).index :
return m
else:
return 'delete'
def tdi(m):
global sell
if m in (sell.tdi.str.lower().value_counts()[sell.tdi.str.lower().value_counts()>10]).index :
return m
else:
return 'delete'
if __name__ == '__main__':
app.run()