def reorder_artists(df, popularity): # calculate lower limit intercept = 0.78 slope = -5.75 lower_limit = max(round((intercept * popularity) + slope), 0) # in case of no popularity set limit to zero # pick artists that exceed lower limit but take only 10 popular_artists = df[df['POPULARITY'] > lower_limit].index.values.tolist()[0:10] print(popular_artists) print(lower_limit) # generate new order if lower limit list was created with meaningful values if len(popular_artists) > 0 and lower_limit > 0: all_artists = df.index.values.tolist() all_artists = [x for x in all_artists if x not in popular_artists] df = df.loc[popular_artists + all_artists][0:25] return df else: df = df[0:25]