简体   繁体   English

熊猫从最近的组中选择所有行

[英]Pandas select all rows from the recent group

I have df:我有df:

id    date    group
1      1.1    3
1      2.1    3
1      3.1    5
1      4.1    5
2      5.2    2
2      6.2    1
2      9.2    1
2      12.2    1 
3      15.3   15
3      20.3    20

I want for each group to get all the rows from the recent date .我希望每个组都从最近的日期获取所有行。 that means that in this df id 2 is the recent group(according to the date column) so I want to filter to display only rows of id 2. So the desire output:这意味着在这个 df id 2 中是最近的组(根据日期列),所以我想过滤以仅显示 id 2 的行。所以期望输出:

id    date    group
1      3.1    5
1      4.1    5
2      6.2    1
2      9.2    1
2      12.2    1 
3      20.3    20

thanks谢谢

this needs 2 steps.这需要 2 个步骤。

  1. get last group per id.获取每个 id 的最后一组。
  2. filter df by 1.按 1 过滤 df。
df = pd.DataFrame(
    data=np.array([[1,1.1,3],[1,2.1,3],[1,3.1,5],[1,4.1,5],[2,5.2,2],[2,6.2,1],[2,9.2,1],[2,12.2,1,],[3,15.3,15],[3,20.3,20]]),
    columns=['id', 'date', 'group']
    )

step 1. get last group per id步骤 1. 获取每个 id 的最后一组

I referred to the following address : Pandas dataframe get first row of each group我参考了以下地址: Pandas dataframe get first row of each group

#step 1
lastgroup = df.groupby('id').last()
lastgroup = lastgroup.reset_index()[['id', 'group']]

lastgroup is :最后一组是:

>>> lastgroup
    id  group
0  1.0    5.0
1  2.0    1.0
2  3.0   20.0

step 2. filter df by lastgroup by using pd.merge :步骤 2. 使用 pd.merge 按 lastgroup 过滤 df:

#step 2
result = pd.merge(left=df, right=lastgroup)

result may be结果可能是

>>> result
    id  date  group
0  1.0   3.1    5.0
1  1.0   4.1    5.0
2  2.0   6.2    1.0
3  2.0   9.2    1.0
4  2.0  12.2    1.0
5  3.0  20.3   20.0

暂无
暂无

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

相关问题 按 ID 选择具有 2 个最近日期的所有行 - Select all rows with 2 most recent dates by ID Pandas:如何从每组中删除选定的行并仅保留最近的行 - Pandas: How to remove selected rows from each group and keep only the recent one 如果所有行都遵循一个序列,如何 select 来自 dataframe 的行组 - how to select group of rows from a dataframe if all rows follow a sequence 如何随机选择每组固定数量的行(如果更大),否则选择熊猫中的所有行? - How to randomly select fixed number of rows (if greater) per group else select all rows in pandas? Pandas select 行基于从特定列中随机选择的组 - Pandas select rows based on randomly selected group from a specific column 如果在组内不满足任何条件,如何 select 所有行,如果在 pandas 中满足组内的某些条件,如何 select 行的子集 - How to select all rows if no conditions are met within a group and select a subset of rows if certain conditions within a group are met in pandas 如果组内的一行满足 pandas 中的特定条件,如何 select 组的所有行 - How to select all rows of group if one row within group meets certain condition in pandas Select Python 中的所有行 pandas - Select all rows in Python pandas PANDAS选择包含最近配对观察的行 - PANDAS select rows that contain most recent observation of pairing 选择pandas中条件为真的所有行 - Select all rows from where a condition is true in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM