简体   繁体   English

Pandas在每组中找到满足条件的最大值

[英]Pandas find the maximum in each group that satisfy a condition

Sorry if this has been asked before, could not find an exact question. 对不起,如果之前有人问过,找不到确切的问题。

I am looking for the most efficient way in Pandas to do the following operation: 我正在寻找Pandas中最有效的方法来执行以下操作:

Lets say we have the following table: 可以说我们有下表:

    ID    SUB_ID    COND

1   101     1        1
2   101     2        1
3   101     3        1
4   102     1        1
5   102     2        0
6   103     1        0
7   103     2        0
8   103     3        0
9   103     4        0

Basically, for each "ID" we want to get the largest "SUB_ID", given that the COND is 1 . 基本上,对于每个“ID”,我们希望得到最大的“SUB_ID”, 假设COND为1 Ideally we would want to add this value to each row of that ID as a new column. 理想情况下,我们希望将此值作为新列添加到该ID的每一行。 If no row of that ID fulfills the condition, we would like to add a 0 (instead of null) 如果该ID的行没有满足条件,我们想添加0(而不是null)

Resulting dataframe would be: 结果数据框将是:

    ID    SUB_ID    COND   MAX_SUB_ID

1   101     1        1         3
2   101     2        1         3
3   101     3        1         3
4   102     1        1         1
5   102     2        0         1
6   103     1        0         0
7   103     2        0         0
8   103     3        0         0
9   103     4        0         0

Best way I can come up with right now is selecting only the rows where COND=1, then doing a groupby on this dataframe to get the max sub id, and then joining it back to the main dataframe. 我现在能想出的最佳方法是仅选择COND = 1的行,然后在此数据帧上执行groupby以获取最大子ID,然后将其连接回主数据帧。 After this I can change the null back to 0. 在此之后,我可以将null更改回0。

df_true = df[df['COND']==1]
max_subid_true=df_true['SUB_ID'].groupby(df_true['ID']).max()

joined_df = df.merge(pd.DataFrame(max_subid_true),how='left',left_on='ID',right_index=True)
joined_df.loc[pd.isnull(joined_df['SUB_ID_y']),'SUB_ID_y']=0 

Any ideas on doing this differently? 有什么不同的想法吗?

df.assign(MAX_SUB_ID=df.SUB_ID.mul(df.COND).groupby(df.ID).transform('max'))

    ID  SUB_ID  COND  MAX_SUB_ID
1  101       1     1           3
2  101       2     1           3
3  101       3     1           3
4  102       1     1           1
5  102       2     0           1
6  103       1     0           0
7  103       2     0           0
8  103       3     0           0
9  103       4     0           0

caveats 注意事项

  • assumes SUB_ID is always positive 假设SUB_ID始终为正
  • assumes COND is always 1 or 0 假设COND始终为10

alternative (with less caveats) 替代方案(减少警告)
but less fun 但不那么有趣

df.assign(MAX_SUB_ID=df.ID.map(df.query('COND == 1').groupby('ID').SUB_ID.max()) \
    .fillna(0).astype(int))

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

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