<jupyter_start><jupyter_text>Building a DataFrame from Name Files<jupyter_code>import pandas as pd

def build_names_df(years):
    columns = ['name', 'sex', 'births','year']
    ldfs = []
    for year in years:
        file_name = 'names/yob'+str(year)+'.txt'
        year_frame = pd.read_csv(file_name, names=columns)
        year_frame['year'] = year
        ldfs.append(year_frame)
    return(pd.concat(ldfs,ignore_index = True))

years = range(1880,2017)
df_names = build_names_df(years)
print(df_names.head())
print(df_names.tail())<jupyter_output>name sex  births  year
0       Mary   F    7065  1880
1       Anna   F    2604  1880
2       Emma   F    2003  1880
3  Elizabeth   F    1939  1880
4     Minnie   F    1746  1880
           name sex  births  year
1891889  Zolton   M       5  2016
1891890  Zurich   M       5  2016
1891891  Zyahir   M       5  2016
1891892    Zyel   M       5  2016
1891893   Zylyn   M       5  2016<jupyter_text>Some statistics<jupyter_code># Total of males and females 
mf = df_names.groupby('sex').births.sum()
print('Number of males:   ',mf.loc['M'])
print('Number of females: ',mf.loc['F'])
# Total of males and females per year
mfy = df_names.pivot_table(index='year',
                               columns='sex', values='births', aggfunc=sum)
print(mfy.head())
import matplotlib.pyplot as plt
% matplotlib inline

mfy.plot()
# compute the frequency (percentage of each name per year) 

names_GByr = df_names.groupby(['year'])
for i in names_GByr: print(i)
# task 2 print the k most frequen names 

# pivot table

name_year = df_names.pivot_table(index='year',columns='name',values='births',aggfunc=sum)
name_year.shape
total_popularity = name_year.groupby(name_year.index)#['name'].sum()
for i in total_popularity:  print(i)<jupyter_output>(1880, name  Aaban  Aabha  Aabid  Aabir  Aabriella  Aada  Aadam  Aadan  Aadarsh  \
year                                                                       
1880    NaN    NaN    NaN    NaN        NaN   NaN    NaN    NaN      NaN   

name  Aaden  ...    Zytaveon  Zytavion  Zytavious  Zyus  Zyva  Zyvion  Zyvon  \
year         ...                                                               
1880    NaN  ...         NaN       NaN        NaN   NaN   NaN     NaN    NaN   

name  Zyyanna  Zyyon  Zzyzx  
year                         
1880      NaN    NaN    NaN  

[1 rows x 96174 columns])
(1881, name  Aaban  Aabha  Aabid  Aabir  Aabriella  Aada  Aadam  Aadan  Aadarsh  \
year                                                                       
1881    NaN    NaN    NaN    NaN        NaN   NaN    NaN    NaN      NaN   

name  Aaden  ...    Zytaveon  Zytavion  Zytavious  Zyus  Zyva  Zyvion  Zyvon  \
year         ...                                                               
1881    Na[...]
{'fraction_non_alphanumeric': 0.08106169296987087, 'fraction_numerical': 0.05308464849354376, 'mean_word_length': 1.7236328125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '270923', 'license': 'MIT', 'n_tokens_mistral': 1069, 'n_tokens_neox': 935, 'n_words': 253, 'path': 'PDS_jupyter/Labs/Lab11 - Pandas_in_Practice .ipynb', 'repo_name': 'formansa/SRF_github', 'size': '2788'}