我有一个类似于以下内容的数据帧date mood score count avg abs23/12/18 negative -50.893 137 -0.371 50.89323/12/18 neutral 0.2193 10 0.0219 0.219323/12/18 positive 336.5098 673 0.5000 336.509824/12/18 positive 91.2414 232 -0.393 91.241424/12/18 neutral 0.063 14 0.0045 0.06324/12/18 negative -649.697 1184 0.5487 649.69725/12/18 negative -72.4142 8 -9.0517 72.414225/12/18 positive 0 0 0 025/12/18 neutral 323.0056 173 1.86708 323.005626/12/18 negative -12.0467 15 -.8031 12.0467我想将以下条件应用于此数据集。Con: if the absolute value(abs) score on a date is the greatest (of 3 moods), keep that date only together with its other attributes. Con: No duplicate date is to be kept. So the dataset will be reduced quite a lot compared to its original size.预期输出date mood_corrected score count avg abs23/12/18 positive 336.5098 673 0.50001456 336.509824/12/18 negative 649.697 1184 0.54873057 649.69725/12/18 neutral 323.0056 173 1.86708439 323.005626/12/18 negative -12.0467 15 -0.8031 12.0467我的代码import pandas as pd df =pd.read_csv('file.csv')new_df= df.sort_values('abs', ascending=False).drop_duplicates(['date','mood'])虽然我得到的结果是根据绝对值**(abs)**对数据集进行排序,但我仍然拥有完整的数据集。它不会减少。任何帮助是值得赞赏的。非常感谢。注意:我查看了堆栈溢出,但没有发现类似的问题。
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
以下将完成这项工作!
new_df = df.sort_values('abs', ascending=False).drop_duplicates(['date']).sort_values('date')
添加回答
举报
0/150
提交
取消