我有类似的情况 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的处理。
答案 0 :(得分:1)
没有比我的建议更明确的答案,我建议使用下面的功能还不错:
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
答案 1 :(得分:0)
如何将Nan值设置为零并创建一个新列var * weight
。然后,您可以使用groupby
来获得结果。
答案 2 :(得分:0)
您可以在调用melt
和调用dropna
之前用apply
和unstack
预处理数据帧
wa=lambda x: np.average(x.value, weights=x.weights)
df_avg = (df.melt(['category', 'weights']).dropna().groupby(['category', 'variable'])
.apply(wa).unstack())
Out[40]:
variable var1 var2
category
a 74.0 71.000000
b 41.0 48.666667
注意:您所需的输出与示例不匹配。 (a, 'var2')
的值为(99 * 9 + 8 * 4) / (9 + 4) = 71