簡體   English   中英

用列組的最大值替換列的 inf 值

[英]replace inf values of a column with the max value of the column group by

我有一個如下所示的數據框

    ID   sales
0   c1   100.0
1   c1    25.0
2   c1    60.0
3   c1    inf
4   c2    40.0
5   c2    inf
6   c3    50.0
7   c3    inf
8   c3    80.0

我想將 sales 列中的 'inf' 替換為 group by ID 列的最大值

所以輸出應該如下所示

  ID    sales
0   c1  100.0
1   c1   25.0
2   c1   60.0
3   c1  100.0
4   c2   40.0
5   c2   40.0
6   c3   50.0
7   c3   80.0
8   c3   80.0

最好的方法是什么?

謝謝

import numpy as np
# skip inf records
max_df = df[df['sales'] != np.inf]
# group by ID without inf
for sales_id, id_df in max_df.groupby('ID'):
    # search in original df by ID + inf and set sales to max value of subgroup
    df.loc[(df['sales'] == np.inf) & (df['ID'] == sales_id), 'sales'] = id_df['sales'].max()

print(df)
#    ID  sales
# 0  c1  100.0
# 1  c1   25.0
# 2  c1   60.0
# 3  c1  100.0
# 4  c2   40.0
# 5  c2   40.0
# 6  c3   50.0
# 7  c3   80.0
# 8  c3   80.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM