繁体   English   中英

Python:每行平均值的广告列

[英]Python: Ad column with average value for each row

我有一个 dataframe 看起来像这样:

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'])
日期 时间类别 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 的总值。

现在我想在这个 dataframe 中添加一列,它显示每天每个 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
日期 时间类别 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,因为我需要 dataframe 的所有行(包括重复行)。

非常感谢您的帮助。

利用:

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  

因此,按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"])))

暂无
暂无

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

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