簡體   English   中英

使用熊貓數據框中的兩列計算加權和

[英]Calculate weighted sum using two columns in pandas dataframe

我正在嘗試使用 python 數據框中的兩列計算加權和。

數據幀結構:

unique_id   weight            value
1           0.061042375       20.16094523
1           0.3064548         19.50932003
1           0.008310739       18.76469039
1           0.624192086       21.25
2           0.061042375       20.23776924
2           0.3064548         19.63366165
2           0.008310739       18.76299395
2           0.624192086       21.25

.......

我想要的輸出是:

每個 unique_id 的加權總和 = sum((weight) * (value))

示例:unique_id 1 的加權和 = ( (0.061042375 * 20.16094523) + (0.3064548 * 19.50932003) + (0.008310739 * 18.76469039) + (0.16094523) + (0.2162)

我查看了這個答案( 使用熊貓/數據框計算加權平均值),但無法找出將其應用於我的特定場景的正確方法。

這就是我根據上述答案所做的:

#Assume temp_weighted_sum_dataframe is the dataframe stated above

grouped_data = temp_weighted_sum_dataframe.groupby('unique_id') #I think this groups data based on unique_id values
weighted_sum_output = (grouped_data.weight * grouped_data.value).transform("sum") #This should allow me to multiple weight and value for every record within each group and sum it up to one value for that group.

# On above line I am getting the error > TypeError: unsupported operand type(s) for *: 'SeriesGroupBy' and 'SeriesGroupBy'

任何幫助表示贊賞,謝謝

鏈接問題中接受的答案確實可以解決您的問題。 但是,我會用一個 groupby 以不同的方式解決它:

u = (df.assign(s=df['weight']*df['value'])
       .groupby('unique_id')
       [['s', 'weight']]
       .sum()
     )

u['s']/u['weight']

輸出:

unique_id
1    20.629427
2    20.672208
dtype: float64

你可以這樣做:

df['partial_sum'] = df['weight']*df['value']
out = df.groupby('unique_id')['partial_sum'].agg('sum')

輸出:

unique_id
1    20.629427
2    20.672208

或者..

df['weight'].mul(df['value']).groupby(df['unique_id']).sum()

相同的輸出

您可以利用agg使用agg@ (這是dot

df.groupby('unique_id')[['weight']].agg(lambda x: x.weight @ x.value)

Out[24]:
              weight
unique_id
1          20.629427
2          20.672208

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM