Home > Enterprise >  Creating a function that group uncommon entries in a column under a alias. Pandas Dataframe, Python
Creating a function that group uncommon entries in a column under a alias. Pandas Dataframe, Python

Time:08-02

I can't get my head around this. I suspect that I'm replacing the text not grouping under a new name. My answer is completely wrong apparently. Please see my code below:

Background on the function: Preserve the more common titles and to group the remaining items into a single category called 'Uncommon'. Write a function called group_titles which takes in your dataframe containing the Title column, along with a selection of uncommon_titles, and returns the dataframe with the modified Title column in which the updated groupings are reflected.

Function parameters needed:

input_df must be Pandas DataFrame. uncommon -> A 1-D list of Python strings representing the categories of titles to be grouped into the "Uncommon" category. Function specifications: Name the function grouped_title Must take any Pandas DataFrame as input and return a DataFrame with the Title column containing newly regrouped categories. Assume that input_df represents a DataFrame possessing a 'Title' column, with each corresponding row entry being a string-based title.

input_df

My code:

def grouped_title(input_df, uncommon):
        input_df[input_df['Title'].isin(uncommon)] = 'Uncommon'
        return input_df

Input function:

grouped_title(title_df, uncommon =['Don.', 'Rev.', 'Mme.','Major.', 'Sir.', 'Mlle.','Countess.', 'Jonkheer.'])['Title'].unique()

My output:

array(['Mr.', 'Mrs.', 'Miss.', 'Master.', 'Uncommon', 'Dr.', 'Ms.',
       'Lady.', 'Col.', 'Capt.'], dtype=object)

Expected output:

['Mr.', 'Mrs.', 'Miss.', 'Master.', 'Uncommon', 'Dr.', 'Ms.',
       'Lady.', 'Col.', 'Capt.']

CodePudding user response:

IIUC, this should work for you:

def grouped_title(input_df, uncommon):
    input_df.loc[input_df['Title'].isin(uncommon), 'Title'] = 'Uncommon'
    return input_df
  • Related