-
Notifications
You must be signed in to change notification settings - Fork 0
/
soc_sec_data.py
294 lines (240 loc) · 7.47 KB
/
soc_sec_data.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
Title: Social Security Data - Baby Names
Description: Work with 140 years of baby names in the US.
Last Updated: April 13, 2023
Developer: Alexander Beck
Email: beckhv2@gmail.com
Github: https://github.com/bexcoding
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ALL_YEARS = np.array(range(1880, 2022))
WOMENS_OLD_TOP_FIVE= ["Mary", "Anna", "Emma", "Elizabeth", "Minnie"]
MENS_OLD_TOP_FIVE = ["John", "William", "James", "Charles", "George"]
def create_df(search_year):
"""
Description
----------
Creates a Pandas dataframe for a specific year using local csv files.
Parameters
----------
search_year: int
Int in range of [1880, 2021]
"""
# replace file directory below with correct local directory
return pd.read_csv(f"/home/nova/Downloads/names/yob{search_year}.txt",
names=["name", "sex", "number"])
def get_gender(df, gender):
"""
Description
----------
Takes a dataframe and returns a dataframe with only male or female entries
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
gender: string
Either "M" for male or "F" for female.
"""
if gender == "M":
return df[df["sex"] == "M"]
elif gender == "F":
return df[df["sex"] == "F"]
def add_number_total(df):
"""
Description
----------
Takes a dataframe and sums up the numbers category. If given a single gender
dataframe, returns total number of males or females.
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
"""
return df["number"].sum()
def top_ten(df, gender, search_year, to_string=False):
"""
Description
----------
Takes an existing dataframe and returns the top ten names for men or women.
If to_string is True, returns a string of the names, else returns np.array.
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
gender: string
Either "M" for male or "F" for female.
search_year: int
Int in range of [1880, 2021].
to_string: bool
Determines if result of function is list or string. Default is False.
"""
def top_ten_string():
return f"The most popular names in {search_year} were " + \
", ".join(names[:-1]) + f", and {names[-1]}."
top_ten_df = get_gender(df, gender).iloc[:10]
names = np.array(top_ten_df["name"])
if to_string == True:
return top_ten_string()
else:
return names
def find_name(df, name):
"""
Description
----------
Returns dataframe rows with a matching name. If only one gender is passed in
then will return one name. If both genders included, can return two names.
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
name: string
A valid name it title case with no extra characters or whitespace.
"""
return df[df["name"] == name]
def find_my_name(df, gender, name):
"""
Description
----------
Returns a numeric rank for a specific name in a specific gender. For
example, a return value of 1 indicates that the name was the number 1 name
in that year for that gender.
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
gender: string
Either "M" for male or "F" for female.
name: string
A valid name it title case with no extra characters or whitespace.
"""
if gender == "F":
rank = (find_name(get_gender(df, "F"), name).index[0]) + 1
return rank
elif gender == "M":
men = get_gender(df, "M")
# men's indexing is affected by all women's indexing beforehand
first_male_index = men.index[0]
rank = (find_name(men, name).index[0] - first_male_index) + 1
return rank
def find_name_over_time(gender, name):
"""
Description
----------
Returs a list of the rank of a name from 1880 to 2021. Separated by gender.
Parameters
----------
gender: string
Either "M" for male or "F" for female.
name: string
A valid name it title case with no extra characters or whitespace.
"""
result_list = []
for y in ALL_YEARS:
print(f"Checking year: {y}")
result_list.append(find_my_name(create_df(y), gender, name))
return result_list
def name_count(df, gender):
"""
Description
----------
Returns the total number of names a gender has in a dataframe.
Parameters
----------
df: Pandas dataframe
A dataframe with "name,sex,number" categories.
gender: string
Either "M" for male or "F" for female.
"""
if gender == "F":
return get_gender(df, "F").last_valid_index()
elif gender == "M":
men = get_gender(df, "M")
return (men.last_valid_index() - men.index[0])
def name_counts_over_time(gender):
"""
Description
----------
Returns a list of the number of different names that a gender had from 1880
to 2021.
Parameters
----------
gender: string
Either "M" for male or "F" for female.
"""
result_list = []
for y in ALL_YEARS:
print(f"Checking year: {y}")
result_list.append(name_count(create_df(y), gender))
return result_list
def plot_name_over_time(gender, name):
"""
Description
----------
Returns a line plot of a name's popularity over time in a given gender.
Parameters
----------
gender: string
Either "M" for male or "F" for female.
name: string
A valid name it title case with no extra characters or whitespace.
"""
y = find_name_over_time(gender, name)
plt.title("Rank (Popularity) of Name Over Time")
plt.xlabel("Year")
plt.ylabel("Rank")
plt.plot(ALL_YEARS, y, label=f"Name={name}")
plt.ylim((max(y) * 1.25) ,0)
plt.xlim(1880, 2021)
plt.legend()
plt.show()
def plot_women_top_five():
"""
Description
----------
Returns a line plot of the change in popularity of the top five most popular
women's names from 1880.
"""
for name in WOMENS_OLD_TOP_FIVE:
y = find_name_over_time("F", name)
plt.plot(ALL_YEARS, y, label=name)
plt.title("Rank (Popularity) of Name Over Time")
plt.xlabel("Year")
plt.ylabel("Rank")
plt.ylim(300 ,0)
plt.xlim(1880, 2021)
plt.legend()
plt.show()
def plot_men_top_five():
"""
Description
----------
Returns a line plot of the change in popularity of the top five most popular
men's names from 1880.
"""
for name in MENS_OLD_TOP_FIVE:
y = find_name_over_time("M", name)
plt.plot(ALL_YEARS, y, label=name)
plt.title("Rank (Popularity) of Name Over Time")
plt.xlabel("Year")
plt.ylabel("Rank")
plt.ylim(200 ,0)
plt.xlim(1880, 2021)
plt.legend()
plt.show()
def plot_name_counts():
"""
Description
----------
Returns a line plot of the change in the number of different names each
gender had over time.
"""
plt.plot(ALL_YEARS, name_counts_over_time("M"), label="Male Names")
plt.plot(ALL_YEARS, name_counts_over_time("F"), label="Female Names")
plt.title("Number of Different Names Over Time \n By Gender")
plt.xlabel("Year")
plt.ylabel("Number of Different Names")
plt.grid()
plt.legend()
plt.show()