我已经编写了一些代码,使用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=',')
我该怎么做?
目前没有回答
相关问题 更多 >
编程相关推荐