数据如图:
得到结果:
方法二:循环计数各个o_town对应cu_pop最大的d_town:
得到结果:
max默认会将所有非group的列作为比较对象,你需要比较的只是cu_pop列d_town不能参与
import pandas
df = pandas.read_excel('test.xlsx')
print(df.groupby('o_town').max(numeric_only=True))
print(df.groupby('o_town').apply(lambda t: t[t.cu_pop == t.cu_pop.max()]))
你的第一种方法,我把数据缩小成几条后,看到问题了吗。
这个聚合后直接max出来的数据都是有问题的
提供参考实例[DataFrame 行列数据筛选实例】,期望对你有所帮助:https://blog.csdn.net/weixin_39631632/article/details/110338730
希望有用
https://blog.csdn.net/weixin_39631632/article/details/110338730