简体   繁体   English

保留最后N个重复的熊猫

[英]Keeping the last N duplicates in pandas

Given a dataframe: 给定一个数据帧:

>>> import pandas as pd
>>> lol = [['a', 1, 1], ['b', 1, 2], ['c', 1, 4], ['c', 2, 9], ['b', 2, 10], ['x', 2, 5], ['d', 2, 3], ['e', 3, 5], ['d', 2, 10], ['a', 3, 5]]
>>> df = pd.DataFrame(lol)

>>> df.rename(columns={0:'value', 1:'key', 2:'something'})
  value  key  something
0     a    1          1
1     b    1          2
2     c    1          4
3     c    2          9
4     b    2         10
5     x    2          5
6     d    2          3
7     e    3          5
8     d    2         10
9     a    3          5

The goal is to keep the last N rows for the unique values of the key column. 目标是为key列的唯一值保留最后N行。

If N=1 , I could simply use the .drop_duplicates() function as such: 如果N=1 ,我可以简单地使用.drop_duplicates()函数:

>>> df.drop_duplicates(subset='key', keep='last')
  value  key  something
2     c    1          4
8     d    2         10
9     a    3          5

How do I keep the last 3 rows for each unique values of key ? 如何为每个唯一的key保留最后3行?


I could try this for N=3 : 我可以尝试N=3

>>> from itertools import chain
>>> unique_keys = {k:[] for k in df['key']}
>>> for idx, row in df.iterrows():
...     k = row['key']
...     unique_keys[k].append(list(row))
... 
>>>
>>> df = pd.DataFrame(list(chain(*[v[-3:] for k,v in unique_keys.items()])))
>>> df.rename(columns={0:'value', 1:'key', 2:'something'})
  value  key  something
0     a    1          1
1     b    1          2
2     c    1          4
3     x    2          5
4     d    2          3
5     d    2         10
6     e    3          5
7     a    3          5

But there must be a better way... 但必须有更好的方法......

Is this what you want ? 这是你想要的吗 ?

df.groupby('key').tail(3)
Out[127]: 
  value  key  something
0     a    1          1
1     b    1          2
2     c    1          4
5     x    2          5
6     d    2          3
7     e    3          5
8     d    2         10
9     a    3          5

Does this help: 这有用吗:

for k,v in df.groupby('key'):
    print v[-2:]

  value  key  something
1     b    1          2
2     c    1          4
  value  key  something
6     d    2          3
8     d    2         10
  value  key  something
7     e    3          5
9     a    3          5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM