Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dataframe列与groupby结果相乘_Python_Pandas_Group By - Fatal编程技术网

Python 将dataframe列与groupby结果相乘

Python 将dataframe列与groupby结果相乘,python,pandas,group-by,Python,Pandas,Group By,我有一个数据帧: cust_sign_month month user country Charge_volume 2018-01 2018-01 10 DE 100 2018-01 2018-01 22 DE 200 2018-01 2018-02

我有一个数据帧:

cust_sign_month      month         user      country      Charge_volume
2018-01              2018-01        10        DE           100 
2018-01              2018-01        22        DE           200              
2018-01              2018-02        33        IN           100              
2019-01              2019-02        42        IN           200             
我需要一个新的列“Volume”,它将根据以下计算结果存储结果:

基于分组国家和月份的“收费量”列的平均值乘以用户列

输出应如下所示:

cust_sign_month      month         user      country      Charge_volume    Volume
2018-01              2018-01        10        DE           100             1500
2018-01              2018-01        22        DE           200             3300 
2018-01              2018-02        33        IN           100             4950 
2019-01              2019-02        42        IN           200             8400
我尝试了以下代码,但似乎不起作用:

df['Volume'] = df.groupby(['month','country'])['Charge_volume'].mean()*df['user']
尝试
groupby()。转换

df['Volume'] = df['user'] * df.groupby(['month','country'])['Charge_volume'].transform('mean')