我是靠谱客的博主 魔幻网络,这篇文章主要介绍pandas算加权平均值_在pandas中使用透视表的加权平均,现在分享给大家,希望可以做个参考。

我已经编写了一些代码,使用pandas中的pivot表计算加权平均值。但是,我不知道如何添加执行加权平均的实际列(添加一个新列,其中每一行包含“cumulative”/“COUNT”值)。

数据看起来是这样的:VALUE COUNT GRID agb

1 43 1476 1051

2 212 1476 2983

5 7 1477 890

4 1361 1477 2310

这是我的代码:# Read input data

lup_df = pandas.DataFrame.from_csv(o_dir+LUP+'.csv',index_col=False)

# Insert a new column with area * variable

lup_df['cumulative'] = lup_df['COUNT']*lup_df['agb']

# Create and output pivot table

lup_pvt = pandas.pivot_table(lup_df, 'agb', rows=['GRID'])

# TODO: Add a new column where each row contains value of 'cumulative'/'COUNT'

lup_pvt.to_csv(o_dir+PIVOT+'.csv',index=True,header=True,sep=',')

我该怎么做?

最后

以上就是魔幻网络最近收集整理的关于pandas算加权平均值_在pandas中使用透视表的加权平均的全部内容,更多相关pandas算加权平均值_在pandas中使用透视表内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部