Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 将Pandas groupby与总计列和总计行一起使用_Python 3.x_Pandas Groupby - Fatal编程技术网

Python 3.x 将Pandas groupby与总计列和总计行一起使用

Python 3.x 将Pandas groupby与总计列和总计行一起使用,python-3.x,pandas-groupby,Python 3.x,Pandas Groupby,在我的代码列表中使用的数据帧属于所有者的资本和收益金额。其目的是按所有者对值进行分组,然后向groupby数据帧添加总计列,然后添加全局总计行 代码如下: import pandas as pd OWNER = 'OWNER' CAPITAL = 'CAPITAL' YIELD = 'YIELD AMT' TOTAL = 'TOTAL' # defining the dataframe df = pd.DataFrame({OWNER: 2 * ['Joe'] + 3 * ['Car

在我的代码列表中使用的数据帧属于所有者的资本和收益金额。其目的是按所有者对值进行分组,然后向groupby数据帧添加总计列,然后添加全局总计行

代码如下:

    import pandas as pd

OWNER = 'OWNER'
CAPITAL = 'CAPITAL'
YIELD = 'YIELD AMT'
TOTAL = 'TOTAL'

# defining the dataframe
df = pd.DataFrame({OWNER: 2 * ['Joe'] + 3 * ['Carla'] + ['Rob'],
                   CAPITAL: [10000, 5000, 20000, 3000, -4000, 2000],
                   YIELD: [1000, 500, 2000, 300, 400, 200]})
'''
   OWNER  CAPITAL  YIELD AMT
0    Joe    10000       1000
1    Joe     5000        500
2  Carla    20000       2000
3  Carla     3000        300
4  Carla    -4000        400
5    Rob     2000        200
'''

print(df)
print()

# grouping the rows by owner
dfg = df.groupby([OWNER]).sum().reset_index()
'''
   OWNER  CAPITAL  YIELD AMT
0  Carla    19000       2700
1    Joe    15000       1500
2    Rob     2000        200
'''

print(dfg)
print()

# adding a TOTAL column
for index in range(0, len(dfg)):
    dfg.loc[index, TOTAL] = dfg.loc[index, CAPITAL] + dfg.loc[index, YIELD]
'''
   OWNER  CAPITAL  YIELD AMT    TOTAL
0  Carla    19000       2700  21700.0
1    Joe    15000       1500  16500.0
2    Rob     2000        200   2200.0
'''

print(dfg)
print()

# resetting index to OWNER column
dfg = dfg.set_index(OWNER)
'''
       CAPITAL  YIELD AMT    TOTAL
OWNER
Carla    19000       2700  21700.0
Joe      15000       1500  16500.0
Rob       2000        200   2200.0
'''

print(dfg)
print()

# finally, adding a TOTAL row
dfg.loc[TOTAL] = dfg.sum(numeric_only=True, axis=0)[[CAPITAL, YIELD, TOTAL]]
'''
       CAPITAL  YIELD AMT    TOTAL
OWNER
Carla  19000.0     2700.0  21700.0
Joe    15000.0     1500.0  16500.0
Rob     2000.0      200.0   2200.0
TOTAL  36000.0     4400.0  40400.0
'''

print(dfg.fillna(''))
我的问题是:有没有一种更简洁的方法可以使用agg()或aggregate()和lambda表达式对整个列或行计算进行编码

df[TOTAL] = df[CAPITAL] + df[YIELD]
output = df.groupby(by=[OWNER]).sum()
这就是你要找的<代码>输出是您需要的数据帧