簡體   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