gpt4 book ai didi

python - 使用 groupby 对多列进行加权平均,逐列删除 NaN

转载 作者:行者123 更新时间:2023-12-03 23:50:11 26 4
gpt4 key购买 nike

我有这样的情况
Pandas Group Weighted Average of Multiple Columns但是其中一列的某些值有时是 NaN。

也就是说,我正在执行以下操作:

import pandas as pd
import numpy as np

df=pd.DataFrame({'category':['a','a','b','b'],
'var1':np.random.randint(0,100,4),
'var2':np.random.randint(0,100,4),
'weights':np.random.randint(0,10,4)})
df.loc[1,'var1']=np.nan
df


category var1 var2 weights
0 a 74.0 99 9
1 a NaN 8 4
2 b 13.0 86 2
3 b 49.0 38 7

def weighted(x, cols, w="weights"):
# Following fails when NaNs might be present:
#return pd.Series(np.average(x[cols], weights=x[w], axis=0), cols)
return pd.Series([np.nan if x.dropna(subset=[c]).empty else np.average(x.dropna(subset=[c])[c], weights =x.dropna(subset=[c])[w] ) for c in cols], cols)

df.groupby('category').apply(weighted, ['var1', 'var2'])


var1 var2
category
a 74.0 57.846154
b 23.0 8.000000

我想要一个更好的方法来做到这一点,但 np.nanmean 不允许权重。 np.average 不允许选项来控制 NaN 的处理。

最佳答案

没有比我的建议更清晰的答案,我建议使用下面的函数还不错:

import pandas as pd
import numpy as np

def weighted_means_by_column_ignoring_NaNs(x, cols, w="weights"):
""" This takes a DataFrame and averages each data column (cols),
weighting observations by column w, but ignoring individual NaN
observations within each column.
"""
return pd.Series([np.nan if x.dropna(subset=[c]).empty else \
np.average(x.dropna(subset=[c])[c],
weights =x.dropna(subset=[c])[w] ) \
for c in cols], cols)

示例用法如下
df=pd.DataFrame({'category':['a','a','b','b'],
'var1':np.random.randint(0,100,4),
'var2':np.random.randint(0,100,4),
'weights':np.random.randint(0,10,4)})
df.loc[1,'var1']=np.nan
df


category var1 var2 weights
0 a 74.0 99 9
1 a NaN 8 4
2 b 13.0 86 2
3 b 49.0 38 7

df.groupby('category').apply(weighted_means_by_column_ignoring_NaNs),
['var1', 'var2'])


var1 var2
category
a 74.0 57.846154
b 23.0 8.000000

关于python - 使用 groupby 对多列进行加权平均,逐列删除 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com