python - 根据值过滤 pandas DataFrame 中的行

标签 python pandas dataframe group-by pandas-groupby

我有类似于下面的 DataFrame(这只是一个示例):

i   TIME    CITIES_LABEL    Value   lat_rounded long

2   2005    Tilburg         22 250  52.070498   4.300700 
3   2005    Amsterdam       45 825  52.370216   4.895168
4   2005    Rotterdam       27 600  51.924420   4.477733
5   2005    Utrecht         12 915  52.090737   5.121420
6   2005    Eindhoven       9 165   51.441642   5.469722
7   2006    Tilburg         7 800   51.560596   5.091914
8   2005    Groningen       7 620   53.219383   6.566502
9   2005    Enschede        6 250   52.221537   6.893662
10  2005    Arnhem          6 025   51.985103   5.898730
11  2006    Utrecht         3 400   50.888174   5.979499
12  2006    Amsterdam       6 795   52.350785   5.264702
13  2005    Breda           8 565   51.571915   4.768323
14  2010    Groningen       6 325   51.812563   5.837226
15  2005    Apeldoorn       7 005   52.211157   5.969923
16  2007    Utrecht         3 785   53.201233   5.799913
17  2006    Rotterdam       7 130   52.387388   4.646219
18  2005    Zaanstad        6 060   52.457966   4.751042
19  2008    Tilburg         6 945   51.697816   5.303675
20  2007    Amsterdam       5 840   52.156111   5.387827
21  2005    Maastricht      5 220   50.851368   5.690972

城市沿 CITIES_LABEL 字段重复。我想根据最高 TIME 值过滤城市。我想要的输出示例是:

i   TIME    CITIES_LABEL    Value   lat_rounded long

6   2005    Eindhoven       9 165   51.441642   5.469722
9   2005    Enschede        6 250   52.221537   6.893662
10  2005    Arnhem          6 025   51.985103   5.898730
13  2005    Breda           8 565   51.571915   4.768323
14  2010    Groningen       6 325   51.812563   5.837226
15  2005    Apeldoorn       7 005   52.211157   5.969923
16  2007    Utrecht         3 785   53.201233   5.799913
17  2006    Rotterdam       7 130   52.387388   4.646219
18  2005    Zaanstad        6 060   52.457966   4.751042
19  2008    Tilburg         6 945   51.697816   5.303675
20  2007    Amsterdam       5 840   52.156111   5.387827
21  2005    Maastricht      5 220   50.851368   5.690972

关于如何最好地在 pandas 中解决这个问题有什么想法吗?

编辑

我的问题不同于Python : How can I get Rows which have the max value of the group to which they belong?因为我正在为 TIMECITIES_LABEL 寻找过滤器,而上一个问题只是查看基于一个字段的(最大)值的过滤,而它没有注意其他领域的重复

最佳答案

使用groupbyidxmax

df.ix[df.groupby('CITIES_LABEL').TIME.idxmax()]

enter image description here

关于python - 根据值过滤 pandas DataFrame 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40289914/

相关文章:

python - 如何临时重定向 Python 中日志记录的输出?

python - 使用预定义的列转换数据帧 Pandas

python - Basic Django,如何为博客的 DetailView 帖子编写 URLConf?

python - 如何在 Matplotlib 中限制水平线的时间范围范围

python - 如何使用 selenium-python 从弹出对话框自动下载文件

pandas.hashtable.PyObjectHashTable.get_item 中的 Python pandas groupby 键错误

python - 将字典项转换为 pandas 数据框的行,其中键是元组,值是整数

python - 极地 : Nesting `over` calls

r - 将具有间隔的单行转换为等于间隔的多行

python - 比较未对齐的系列列 Pandas