-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (147 loc) · 6.36 KB
/
main.py
File metadata and controls
178 lines (147 loc) · 6.36 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
import pandas as pd
import numpy as np
import pymysql
from sqlalchemy import create_engine
import requests
from bs4 import BeautifulSoup
#get raw data#
engine = create_engine('sqlite:////Users/stefaniabraca/Ironhack-Module-1-Project-hatching/data/stefaniabraca.db')
query = "select * from personal_info"
dfpersonal_info = pd.read_sql_query(query, engine)
query = "select * from business_info"
dfbusiness_info = pd.read_sql_query(query, engine)
query = "select * from rank_info"
dfrank_info = pd.read_sql_query(query, engine)
forbes_df = pd.read_csv('~/Ironhack-Module-1-Project-hatching/data/forbes_2018.csv')
tertiary_ed = pd.read_csv('~/Ironhack-Module-1-Project-hatching/data/tertiary_ed.csv')
#data wrangling#
col_todrop = [ 'worthChange','lastName', 'Unnamed: 0_x', 'Unnamed: 0_y', 'realTimeWorth', 'realTimePosition', 'Unnamed: 0', 'image']
reindex_col = ['position', 'worth', 'name', 'age', 'gender', 'Source', 'country']
#web scrapping#
url = 'https://en.wikipedia.org/wiki/The_World%27s_Billionaires'
html = requests.get(url).content
soup = BeautifulSoup(html, "lxml")
######## acquire ########
def get_main_data(df1, df2, df3):
merged_df = df1.merge(df2, on='id')
raw_df = merged_df.merge(df3, on='id')
return raw_df
######## wrangling ########
def drop(df, list_col_todrop):
drop
df.drop(list_col_todrop, axis=1, inplace=True)
cols = list(df.columns.values)
cols = ['id', 'name', 'position', 'Source', 'worth', 'age', 'gender', 'country']
df = df.reindex(columns=cols)
df.name = df.name.str.title()
return df
def reindex (df, listt):
df = df.reindex(columns=listt)
newlist = [w.title() for w in listt]
df.columns = newlist
return df
def gender (df, index1, index2):
df.loc[index1, 'Gender']='Female'
df.loc[index2, 'Gender']='Female'
df["Gender"].fillna("Male", inplace = True)
df.astype({'Gender': str}).dtypes
return df
def gender_cat (df):
df.loc[df['Gender'].str.startswith('M'), 'Gender_cat'] = 'Male'
df.loc[df['Gender'].str.startswith('F'), 'Gender_cat'] = 'Female'
df[['Gender_cat']] = df[['Gender_cat']].fillna('Not Available')
df.drop('Gender', axis=1, inplace=True)
return df
def year_toage(df):
df['Age'] = df['Age'].str.replace('years old', '')
df['Age'] = df['Age'].str.replace('1982', '36')
df['Age'] = df['Age'].str.replace('1983', '35')
df['Age'] = df['Age'].str.replace('1984', '34')
df['Age'] = df['Age'].str.replace('1985', '33')
df['Age'] = df['Age'].str.replace('1986', '32')
df['Age'] = df['Age'].str.replace('1987', '31')
df['Age'] = df['Age'].str.replace('1988', '30')
df['Age'] = df['Age'].str.replace('1989', '29')
df['Age'] = df['Age'].str.replace('1990', '28')
df['Age'] = df['Age'].str.replace('1991', '27')
df['Age'] = df['Age'].str.replace('1992', '26')
df['Age'] = df['Age'].str.replace('1994', '24')
df['Age'] = df['Age'].str.replace('1996', '22')
df['Age'] = df['Age'].str.replace('1998', '20')
return df
def nan_toage (df):
df[['Age']] = df[['Age']].fillna(0)
df.Age = df.Age.astype(int)
mean= df['Age'].mean()
df['Age'] = np.where(df['Age'] == 0, df.Age.replace(0,'mean'), df['Age'])
return df
def clean_country (df):
df.Country = df.Country.astype(str)
df['Country'] = df['Country'].str.replace('USA', 'United States')
df['Country'] = df['Country'].str.replace("People's Republic of China", 'China')
return df
######## enrichment ########
def scr_top10 (url, html, soup):
table = soup.find_all('table',{'class':'wikitable sortable'})[0]
return table
def scr_table(table):
rows = table.find_all('tr')
rows_parsed = [row.text for row in rows]
rows = [row.text.strip().split("\n") for row in rows]
colnames = rows[0]
for ele in colnames:
if ele == (''):
colnames.remove(ele)
data = rows[1:]
top10_df = pd.DataFrame(data, columns=colnames)
top10_df.drop(columns=['Name', 'Net worth (USD)', 'Age', 'Source(s) of wealth'], inplace=True)
top10_df.rename(columns={"No.": "Position"}, inplace=True)
return top10_df
def merge1 (df1,df2):
df1.Position = df1.Position.astype(int)
merged_df = pd.merge(df1, df2, on='Position', how='outer')
merged_df['Country'] = np.where(merged_df['Country'] == 'None', merged_df['Nationality'], merged_df['Country'])
merged_df[['Country']] = merged_df[['Country']].fillna('None')
return merged_df
def clean_forbesdf (df):
df.sort_values(by=['position'])
null_displ = df[(df['position'].isnull()==True)]
df.dropna(subset=['position'], inplace=True)
df.position.astype(int)
df.drop(columns=['lastName', 'age', 'gender', 'wealthSource', 'industry', 'worthChange', 'realTimeWorth', 'realTimePosition'], inplace=True)
df.drop(columns=['worth', 'image'], inplace=True)
df.rename(columns={"position": "Position"}, inplace=True)
return df
def merge2 (df1,df2):
merged2_df = pd.merge(df1, df2, on='Position', how='outer')
merged2_df['Country'] = np.where(merged2_df['Country'] == 'None', merged2_df['country'], merged2_df['Country'])
return merged2_df
def cleancountry (merged2_df):
merged2_df['Country'] = merged2_df['Country'].str.replace('\xa0United States', 'United States')
merged2_df['Country'] = merged2_df['Country'].str.replace("People's Republic of China", 'China')
merged2_df['Country'] = merged2_df['Country'].str.replace('\xa0Spain', 'Spain')
merged2_df["Country"]= merged2_df["Country"].astype(str)
merged2_df.drop(columns=['Nationality', 'name','country'], inplace=True)
return merged2_df
######### PIPELINE ########
def main ():
raw_df = get_main_data(dfpersonal_info, dfbusiness_info, dfrank_info)
final_df = drop(raw_df, col_todrop)
final_df.name = final_df.name.str.title()
final_df = reindex(final_df, reindex_col)
final_df = gender(final_df, 26, 596)
final_df = gender_cat(final_df)
final_df = year_toage(final_df)
final_df = nan_toage(final_df)
final_df.Country = final_df.Country.astype(str)
wrangled_df = clean_country(final_df)
table = scr_top10(url, html, soup)
top10_df = scr_table(table)
top10_df = top10_df.astype({"Position": int})
merged_df = merge1(wrangled_df, top10_df)
clean_forbes_df = clean_forbesdf(forbes_df)
merged2_df = merge2(merged_df, clean_forbes_df)
merged2_df = cleancountry(merged2_df)
print(merged2_df)
if __name__ == "__main__":
main()