In: Computer Science





#code in text
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import numpy as np
import pandas as pd
import seaborn
import matplotlib.pyplot as plt
import os
# In[4]:
cd names/
# In[7]:
os.listdir("C:/Users/Gopal/Desktop/names")
#Each .txt file contains Name, Sex, and the number of babies born
that year(1880 to 2018) with that name
# In[10]:
# read any of them
open(os.listdir("C:/Users/Gopal/Desktop/names")[0],'r').readlines()[:8]
# In[38]:
# collect each year of data
All_year_data=[]
# loop over all file in listdir
Year=1880
for ix in os.listdir("C:/Users/Gopal/Desktop/names") :
All_year_data.append(pd.read_csv(ix,names=['name','sex','number']))
All_year_data[-1]['year'] = Year
Year=Year+1
final_data=pd.concat(All_year_data)# Concatenate every year
data
final_data.head(n=100)
# In[39]:
#Generate indexed data.
final_data =
final_data.set_index(['sex','name','year']).sort_index(level=0)
# In[40]:
final_data.head()
# In[44]:
def popularname(sex,name):
new_data=final_data.loc[sex,name]
#print(new_data)
plt.plot(new_data.index,new_data.values)
# In[51]:
# plot the fig
plt.figure(figsize=(8,8))
plt.title("Popular Name")
# define the dictonary to store name and sex to plot the
fig
dic={"Penny":"F",
"Ken":"M"
}
# function call for plot given dictonary
for name,sex in dic.items():
popularname(sex,name)
plt.legend(dic.keys())
plt.xlabel("Y ear")
plt.ylabel("no of time name occure")
plt.show()
plt.savefig("name.png")