【问题标题】:Python: Ad column with average value for each rowPython:为每行添加具有平均值的列
【发布时间】:2021-12-21 08:28:30
【问题描述】:

我有一个如下所示的数据框:

data1 = [['2020-10-01', '07-08', 3.0 ], ['2020-10-01', '08-09', 2.0], ['2020-10-01', '07-08', 3.0], ['2020-10-01', '07-08', 3.0],['2020-10-02', '07-08', 3.0 ], ['2020-10-02', '08-09', 3.0], ['2020-10-02', '07-08', 3.0], ['2020-10-02', '08-09', 3.0],  ['2020-10-03', '09-10', 9.0], ['2020-10-03', '09-10', 9.0]]
  
df1 = pd.DataFrame(data1, columns = ['Date', 'TimeCategory', 'Value_TimeCategory_total'])
Date TimeCategory Value_TimeCategory_total
2020-10-01 07-08 3.0
2020-10-01 08-09 2.0
2020-10-01 07-08 3.0
2020-10-01 07-08 3.0
2020-10-02 07-08 3.0
2020-10-02 08-09 3.0
2020-10-02 07-08 3.0
2020-10-02 08-09 3.0
2020-10-03 09-10 9.0
2020-10-03 09-10 9.0

Dataframe 包含一天内每个 TimeCategory 的总值。

现在我想在此数据框中添加一列,显示每天每个 TimeCategory 的平均值。

如果我有 3 行日期为 2020-10-01 且 TimeCategory 为 07-08 且总值等于 3.0,我希望平均值等于 1.0。

结果应该是这样的。

data2 = [['2020-10-01', '07-08', 3.0 , 1.0], ['2020-10-01', '08-09', 2.0, 2.0], ['2020-10-01', '07-08', 3.0, 1.0], ['2020-10-01', '07-08', 3.0, 1.0],['2020-10-02', '07-08', 3.0, 1.5 ], ['2020-10-02', '08-09', 3.0, 1.5], ['2020-10-02', '07-08', 3.0, 1.5], ['2020-10-02', '08-09', 3.0, 1.5], ['2020-10-03', '09-10', 9.0, 4.5], ['2020-10-03', '09-10', 9.0, 4.5]]
  
df2 = pd.DataFrame(data2, columns = ['Date', 'TimeCategory', 'Value_TimeCategory_total' , 'Value_TimeCategory_Row_Average'])
  
df2
Date TimeCategory Value_TimeCategory_total Value_TimeCategory_Row_Average
2020-10-01 07-08 3.0 1.0
2020-10-01 08-09 2.0 2.0
2020-10-01 07-08 3.0 1.0
2020-10-01 07-08 3.0 1.0
2020-10-02 07-08 3.0 1.5
2020-10-02 08-09 3.0 1.5
2020-10-02 07-08 3.0 1.5
2020-10-02 08-09 3.0 1.5
2020-10-03 09-10 9.0 4.5
2020-10-03 09-10 9.0 4.5

我不想使用 group by,因为我需要数据框的所有行(包括重复行)。

非常感谢您的帮助。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    想法是将Value_TimeCategory_total 列除以GroupBy.transform 中的每组计数,以获得与原始大小相同的Series

    df1['Value_TimeCategory_Row_Average'] = (df1['Value_TimeCategory_total']
                    .div(df1.groupby(['Date','TimeCategory'])['Value_TimeCategory_total']
                    .transform('size')))
    print (df1)
    
             Date TimeCategory  Value_TimeCategory_total  \
    0  2020-10-01        07-08                       3.0   
    1  2020-10-01        08-09                       2.0   
    2  2020-10-01        07-08                       3.0   
    3  2020-10-01        07-08                       3.0   
    4  2020-10-02        07-08                       3.0   
    5  2020-10-02        08-09                       3.0   
    6  2020-10-02        07-08                       3.0   
    7  2020-10-02        08-09                       3.0   
    8  2020-10-03        09-10                       9.0   
    9  2020-10-03        09-10                       9.0   
    
       Value_TimeCategory_Row_Average  
    0                             1.0  
    1                             2.0  
    2                             1.0  
    3                             1.0  
    4                             1.5  
    5                             1.5  
    6                             1.5  
    7                             1.5  
    8                             4.5  
    9                             4.5  
    

    替代解决方案:

    df1['Value_TimeCategory_Row_Average'] = (df1.groupby(['Date','TimeCategory'])['Value_TimeCategory_total']
                                                .transform(lambda x: x / len(x)))
    

    【讨论】:

    • 非常感谢,这正是我所需要的!
    【解决方案2】:

    因此,按Date, TimeCategory 分组,其他单元格分别具有相同的值。 我不认为groupby 不一定有助于实现你所需要的——你只需要将它与assign 结合起来:

    df2.set_index(["Date", "TimeCategory"], inplace=True)
    
    df2 = df2.assign(Value_TimeCategory_Row_Average = df2.groupby(["Date", "TimeCategory"]).apply(lambda x:x["Value_TimeCategory_total"].mean() / len(x["Value_TimeCategory_total"])))
    

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多