Generation

generate functionWed, 01 Feb 2023

There is a Pandas dataframe: song performer chart_debut peak_position worst_position time_on_chart consecutive_weeks 261636 Stupid Cupid Connie Francis 1958-08-02 17 72 12 11.0 46383 Chantilly Lace Big Bopper 1958-08-02 6 40 18 17.0 46384 Chantilly Lace Big Bopper 1958-08-02 6 40 19 18.0 46385 Chantilly Lace Big Bopper 1958-08-02 6 40 20 19.0 46386 Chantilly Lace Big Bopper 1958-08-02 6 40 21 20.0 Pandas dataframe is available: song artist chart_debut peak_position worst_position time_on_chart consecutive_weeks 261636 Stupid Cupid Connie Francis 1958-08-02 17 72 12 11.0 46383 Chantilly Lace Big Bopper 1958-08-02 6 40 18 17.0 46384 Chantilly Lace Big Bopper 1958-08-02 6 40 19 18.0 46385 Chantilly Lace Big Bopper 1958-08-02 6 40 20 19.0 46386 Chantilly Lace Big Bopper 1958-08-02 6 40 21 20.0 Write a function that will group all rows by the performer column (so that no duplicates are left), and all corresponding (unique) values to that column are written through the "," sign into a new hits column.

def group_by_performer(df): # make a copy of df df_copy = df.copy() # sort by performer df_copy.sort_values(by='performer', inplace=True) # group by song and take the unique performers df_copy['hits'] = df_copy.groupby('song')['performer'].transform(lambda x: ','.join(x.unique())) # drop duplicates and reset index df_copy.drop_duplicates(subset='performer', inplace=True) df_copy.reset_index(inplace=True) return df_copy group_by_performer(df)

Questions about programming?Chat with your personal AI assistant