Generation

generate functionWed, 01 Feb 2023

There is a Pandas dataframe: performer hits chart_debut time_on_chart consecutive_weeks 8478 Taylor Swift Fifteen, Fearless, London Boy, Teardrops On My... 2008 14299 11880.0 3819 Imagine Dragons Radioactive, Natural, Believer, Thunder, On To... 2012 11101 9304.0 2674 Ed Sheeran Thinking Out Loud, Photograph, Sing, Don't, On... 2014 10685 10039.0 9590 The Weeknd Starry Eyes, Save Your Tears, Acquainted, Blin... 2022 9429 6713.0 5812 Mariah Carey Without You/Never Forget You, Don't Forget Abo... 1994 8735 6616.0 5749 Madonna Like A Prayer, Erotica, Vogue, Lucky Star, Cau... 1989 8391 7534.0 Write a function num_of_hits that will sort the dataframe by the number of values (separated by commas) in the hits column

def num_of_hits(df): df['num_of_hits'] = df.hits.apply(lambda x: len(x.split(', '))) return df.sort_values(by='num_of_hits', ascending=False) num_of_hits(df)

Questions about programming?Chat with your personal AI assistant