text_plain_output_1.png image_output_1.png
from sklearn.preprocessing import MinMaxScaler

import pandas as pd

df = pd.read_csv('../input/NationalNames.csv')

df['Decade'] = df['Year'].apply(lambda x: x - x % 10)

df_pivot = df.pivot_table(values='Count', index=['Decade', 'Name', 'Gender'], aggfunc='sum')
new_df = pd.DataFrame()
new_df['Decade'] = df_pivot.index.get_level_values('Decade')
new_df['Name'] = df_pivot.index.get_level_values('Name')
new_df['Gender'] = df_pivot.index.get_level_values('Gender')
new_df['Count'] = df_pivot.values

decadeList = list(new_df['Decade'].unique())
boys_percentileList = []
girls_percentileList = []
boys_df = new_df[new_df['Gender'] == 'M'].copy()
girls_df = new_df[new_df['Gender'] == 'F'].copy()
for i in decadeList:
    scaler = MinMaxScaler()
    boys_percentileList.extend(scaler.fit_transform(boys_df[boys_df['Decade'] == i][['Count']]))
    girls_percentileList.extend(scaler.fit_transform(girls_df[girls_df['Decade'] == i][['Count']]))
boys_df['decade_percentile'] = boys_percentileList
girls_df['decade_percentile'] = girls_percentileList
new_df = boys_df.append(girls_df)
new_df['decade_percentile'] = new_df['decade_percentile'].apply(lambda x: float(x) * 100)
new_df.sort_index(inplace=True)
del boys_df
del girls_df

def nameFilter(decade, gender, lowerBound, upperBound, startsWith=None):
    """
        This function helps you find rare/common baby names!
        Inputs:
            decade : integer = Decade as a 4 digit number, e.g. 1980.
            gender : string = Gender as a single letter string, e.g. 'M' for Male
            lowerBound: float = Lower percentage of the names you want to query, e.g. 25 for 25%, NOT 0.25
            upperBound: float = Upper percentage of the names you want to query
            startsWith: str = (Optional) Single letter representing the starting letter of a name
        Returns:
            A dataframe slice fitting your parameters.
    """
    if upperBound < lowerBound:
        raise ValueError('lowerBound needs to be less than upperBound')
    if startsWith != None:
        result_df = new_df[(new_df['Decade'] == decade) & (new_df['Gender'] == gender) & (new_df['decade_percentile'] >= lowerBound) & (new_df['decade_percentile'] <= upperBound) & (new_df['Name'].str[0] == startsWith.upper())]
    else:
        result_df = new_df[(new_df['Decade'] == decade) & (new_df['Gender'] == gender) & (new_df['decade_percentile'] >= lowerBound) & (new_df['decade_percentile'] <= upperBound)]
    return result_df

nameFilter(decade=1980, gender='M', lowerBound=50, upperBound=100, startsWith='C')