Py学习  »  Sacha Bernheim  »  全部回复
回复总数  1

你可以试试这样的方法:

# sort dataframe
df.sort_values(by=['count'], inplace=True)
# recreate the index of your rows to make sure that 0 corresponds to the one with the higher count
df.reset_index(drop=True, inplace=True)
# add your new row to your dataset
df.append({'useragent': 'Others', 'count': df.loc[5:]['count'].cumsum()}, inplace=True)
# drop the rows you don't need anymore
df.drop([5:len(df.index.values.tolist())-1], inplace=True)

我不完全确定,但值得一试。我希望它能给你一些建议。