推荐的方法是计算 pandas 数据帧中选定列的加权总和? [英] What is the recommended way to compute a weighted sum of selected columns of a pandas dataframe?

查看:77
本文介绍了推荐的方法是计算 pandas 数据帧中选定列的加权总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想为下面的矩阵计算列"a"和"c"的加权总和,其权重在字典w中定义.

For example, I would like to compute the weighted sum of columns 'a' and 'c' for the below matrix, with weights defined in the dictionary w.

df = pd.DataFrame({'a': [1,2,3], 
                   'b': [10,20,30], 
                   'c': [100,200,300],
                   'd': [1000,2000,3000]})
w = {'a': 1000., 'c': 10.}

我自己想出了一些选项(见下文),但所有选项看起来都有些复杂.这个基本用例是否没有直接的熊猫操作?像df.wsum(w)一样?

I figured out some options myself (see below), but all look a bit complicated. Isn't there a direct pandas operation for this basic use-case? Something like df.wsum(w)?

我尝试了 pd.DataFrame.dot ,但会引发值错误:

I tried pd.DataFrame.dot, but it raises a value error:

df.dot(pd.Series(w))
# This raises an exception:
# "ValueError: matrices are not aligned"

可以通过为每列指定权重来避免异常,但这不是我想要的.

The exception can be avoided by specifying a weight for every column, but this is not what I want.

w = {'a': 1000., 'b': 0., 'c': 10., 'd': 0. }
df.dot(pd.Series(w)) # This works

一个人如何只计算列的子集上的点积?或者,可以在应用点运算之前选择感兴趣的列,或者利用以下事实:当计算(按行)和时,pandas/numpy忽略nan(请参见下文).

How can one compute the dot product on a subset of columns only? Alternatively, one could select the columns of interest before applying the dot operation, or exploit the fact that pandas/numpy ignores nans when computing (row-wise) sums (see below).

以下是我能够发现自己的三种方法:

Here are three methods that I was able to spot out myself:

w = {'a': 1000., 'c': 10.}

# 1) Create a complete lookup W.
W = { c: 0. for c in df.columns }
W.update(w)
ret = df.dot(pd.Series(W))

# 2) Select columns of interest before applying the dot product.
ret = df[list(w.keys())].dot(pd.Series(w))

# 3) Exploit the handling of NaNs when computing the (row-wise) sum
ret = (df * pd.Series(w)).sum(axis=1)
# (df * pd.Series(w)) contains columns full of nans

我是否缺少选择?

推荐答案

您可以像在第一个示例中那样使用系列,然后再使用reindex:

You could use a Series as in your first example, just use reindex afterwards:

import pandas as pd

df = pd.DataFrame({'a': [1,2,3],
                   'b': [10,20,30],
                   'c': [100,200,300],
                   'd': [1000,2000,3000]})

w = {'a': 1000., 'c': 10.}
print(df.dot(pd.Series(w).reindex(df.columns, fill_value=0)))

输出

0    2000.0
1    4000.0
2    6000.0
dtype: float64

这篇关于推荐的方法是计算 pandas 数据帧中选定列的加权总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆