我有一個如下所示的資料框:
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 的總值。
現在我想在這個資料框中添加一列,它顯示每天每個 TimeCategory 的平均值。
如果我有 3 行,日期為 2020-10-01,時間類別為 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,因為我需要資料幀的所有行(包括重復行)。
非常感謝您的幫助。
uj5u.com熱心網友回復:
想法是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)))
uj5u.com熱心網友回復:
因此,按 分組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"])))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/389514.html
上一篇:如何在scala中獲取分層陣列的最后一個元素并對其應用聚合函式?
下一篇:從列中提取某些單詞
