Generation

generate functionWed, 01 Feb 2023

There is a function for Pandas dataframe: dfp = df.copy() dfp.sort_values(by='performer', inplace=True) def group_by_performer(data): dfp['hits'] = dfp.groupby('performer')['song'].transform(lambda x: ', '.join(x.unique())) dfp.drop_duplicates(subset='performer', inplace=True) dfp.reset_index(inplace=True) return dfp group_by_performer(dfp) Add to it the function of sum of all values in the time_on_chart and consecutive_weeks columns if the values in the performer column are the same. The total should be displayed in the same columns (time_on_chart and consecutive_weeks)

dfp = df.copy() dfp.sort_values(by='performer', inplace=True) def group_by_performer(data): dfp['hits'] = dfp.groupby('performer')['song'].transform(lambda x: ', '.join(x.unique())) dfp['time_on_chart'] = dfp.groupby('performer')['time_on_chart'].transform(lambda x: x.sum()) dfp['consecutive_weeks'] = dfp.groupby('performer')['consecutive_weeks'].transform(lambda x: x.sum()) dfp.drop_duplicates(subset='performer', inplace=True) dfp.reset_index(inplace=True) return dfp group_by_performer(dfp)

Questions about programming?Chat with your personal AI assistant