Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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/2/sharepoint/4.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 分组值和求和&;新数据帧-使用熊猫_Python_Pandas_Dataframe_Pandas Groupby - Fatal编程技术网

Python 分组值和求和&;新数据帧-使用熊猫

Python 分组值和求和&;新数据帧-使用熊猫,python,pandas,dataframe,pandas-groupby,Python,Pandas,Dataframe,Pandas Groupby,今天,我一直在寻找一个答案,但我尝试的每一个变体都不起作用 问题陈述-将下面“月”列的总数相加,使其显示一次,并创建一个新的数据框: 我已经尝试过多种groupby选项,但最终还是产生了错误 现在存在: ╔═══════╦════════════╦═════════╗ ║ Month ║ Withdrawal ║ Deposit ║ ╠═══════╬════════════╬═════════╣ ║ 10 ║ 50 ║ 40 ║ ║

今天,我一直在寻找一个答案,但我尝试的每一个变体都不起作用

问题陈述-将下面“月”列的总数相加,使其显示一次,并创建一个新的数据框:

我已经尝试过多种groupby选项,但最终还是产生了错误

现在存在:

   ╔═══════╦════════════╦═════════╗
   ║ Month ║ Withdrawal ║ Deposit ║
   ╠═══════╬════════════╬═════════╣
   ║    10 ║         50 ║      40 ║
   ║    10 ║         50 ║      60 ║
   ║     6 ║         25 ║      10 ║
   ║     6 ║         30 ║      10 ║
   ║     3 ║        125 ║      60 ║
   ║     2 ║        100 ║      10 ║
   ╚═══════╩════════════╩═════════╝
期望输出:

   ╔═══════╦════════════╦═════════╗
   ║ Month ║ Withdrawal ║ Deposit ║
   ╠═══════╬════════════╬═════════╣
   ║    10 ║        100 ║     100 ║
   ║     6 ║         55 ║      20 ║
   ║     3 ║        125 ║      60 ║
   ║     2 ║        100 ║      10 ║
   ╚═══════╩════════════╩═════════╝
编辑:

这是我的代码-它来自另一个数据帧:

mf = pd.DataFrame()
mf['Month'] = df['Month']
mf['Withdrawals'] = df['Withdrawals']
mf['Deposits'] = df['Deposits']
mf['Month'] = mf.groupby('Month', as_index=False).sum()
print(mf)
当我这样做时,它会输出以下内容:

    ╔═══════╦═════════════╦══════════╗
    ║ Month ║ Withdrawals ║ Deposits ║
    ╠═══════╬═════════════╬══════════╣
    ║     1 ║ 206.94      ║ 300      ║
    ║     2 ║ 256.5       ║ 10.12    ║
    ║     3 ║ 40          ║ 52.31    ║
    ║     4 ║ 150         ║ 15.99    ║
    ║     5 ║ 1486.86     ║ 15.99    ║
    ║   755 ║ NaN         ║ NaN      ║
    ║   756 ║ NaN         ║ NaN      ║
    ║   757 ║ NaN         ║ NaN      ║
    ║   758 ║ NaN         ║ NaN      ║
    ║   759 ║ NaN         ║ NaN      ║
    ╚═══════╩═════════════╩══════════╝

因此,从输出来看,它没有将它们全部放在第1-12个月…

df.groupby('month',as\u index=False)。sum()
?不起作用,但添加了上面的公式以使其更清晰。不,不要分配任何内容,只需
mf=df.groupby('month',as\u index=False)。sum()
。得到了它,它解决了问题。分配就是把事情搞砸的原因。