【问题标题】:Fill NAs : Min of values in group填充 NAs:组中的最小值
【发布时间】:2023-01-18 18:02:53
【问题描述】:

这是我的数据框。

df = pd.DataFrame ( {'CNN': ['iphone 11 63 GB TMO','iphone 11 128 GB ATT','iphone 11 other carrier','iphone 12 256 GB TMO','iphone 12 64 GB TMO','iphone 12 other carrier'], 
                            'Family Name':['iphone 11', 'iphone 11', 'iphone 11', 'iphone 12', 'iphone 12', 'iphone 12'],
                            'Storage': [63, 128,np.nan, 256,64, np.nan]})
Output:

                       CNN Family Name  Storage
0      iphone 11 63 GB TMO   iphone 11     63.0
1     iphone 11 128 GB ATT   iphone 11    128.0
2  iphone 11 other carrier   iphone 11      NaN
3     iphone 12 256 GB TMO   iphone 12    256.0
4      iphone 12 64 GB TMO   iphone 12     64.0
5  iphone 12 other carrier   iphone 12      NaN

我想要实现的是找到 NA。标准是来自组(姓氏)的最小存储量。我试图按 fillna(min()) 分组,但它似乎没有用。

#Tried
df["Storage"] = df.groupby("Family Name").apply(lambda x: x.fillna(x.min()))

这是预期的最终输出。

Expected Output:

                       CNN Family Name  Storage
0      iphone 11 63 GB TMO   iphone 11     63.0
1     iphone 11 128 GB ATT   iphone 11    128.0
2  iphone 11 other carrier   iphone 11     63.0
3     iphone 12 256 GB TMO   iphone 12    256.0
4      iphone 12 64 GB TMO   iphone 12     64.0
5  iphone 12 other carrier   iphone 12     64.0

【问题讨论】:

    标签: python pandas fillna


    【解决方案1】:

    groupby.transformfillna 一起使用:

    df['Storage'] = df['Storage'].fillna(df.groupby('Family Name')['Storage'].transform('min'))
    

    或者,groupby.minmapboolean indexing 可能更有效,具体取决于 DataFrame 的大小、NaN 的数量(很少)和组的数量:

    s = df.groupby('Family Name')['Storage'].min()
    m = df['Storage'].isna()
    df.loc[m, 'Storage'] = df.loc[m, 'Family Name'].map(s)
    

    输出:

                           CNN Family Name  Storage
    0      iphone 11 63 GB TMO   iphone 11     63.0
    1     iphone 11 128 GB ATT   iphone 11    128.0
    2  iphone 11 other carrier   iphone 11     63.0
    3     iphone 12 256 GB TMO   iphone 12    256.0
    4      iphone 12 64 GB TMO   iphone 12     64.0
    5  iphone 12 other carrier   iphone 12     64.0
    

    【讨论】:

      【解决方案2】:

      GroupBy.transform用于与Series.fillna具有相同大小的原始列的系列:

      df["Storage"] = df["Storage"].fillna(df.groupby("Family Name")["Storage"].transform('min'))
      

      您的解决方案应该更改:

      df["Storage"] = df.groupby("Family Name")["Storage"].transform(lambda x: x.fillna(x.min()))
      

      【讨论】:

      • Downvote,downvote 的原因是什么?
      • @mozway - 没关系。
      【解决方案3】:

      另一种可能的解决方案:

      (df.groupby('Family Name', group_keys=True, as_index=False)
       .apply(lambda g: g.sort_values('Storage', ascending=False).ffill().sort_index())
       .reset_index(drop=True))
      

      输出:

                             CNN Family Name  Storage
      0      iphone 11 63 GB TMO   iphone 11     63.0
      1     iphone 11 128 GB ATT   iphone 11    128.0
      2  iphone 11 other carrier   iphone 11     63.0
      3     iphone 12 256 GB TMO   iphone 12    256.0
      4      iphone 12 64 GB TMO   iphone 12     64.0
      5  iphone 12 other carrier   iphone 12     64.0
      

      【讨论】:

        猜你喜欢
        • 2022-06-30
        • 2014-02-16
        • 2014-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        • 2018-12-13
        相关资源
        最近更新 更多