希望通过使用group by遍历区域列(4个区域),然后运行一堆数据透视表,这些数据透视表将彼此堆叠并创建一个总行。数据透视表需要在我分组的所有4个区域上运行。一旦有了枢轴,就需要将它们堆叠在一起。
import pandas as pd
import numpy as np
df = pd.DataFrame({'Roll': ['Analyst','doctor','activist','lawyer','writer','manager'],
'Animal': ['cats','dogs','birds','pianos','elephant','dinos'],
'Region': ['EM', 'US', 'US', 'Europe', 'Asia', 'Asia'],
'Year': ['2006', '2010', '2013', '2010', '2002', '2003'],
'Dollar Amount': [10, 20, 30, 40, 50, 60]})
years = ['2001', '2002', '2003', '2004', '2005']
dfs = {}
for region, df_region in df.groupby('Region'):
filter = df[df['Roll'].isin(Analyst) & df['Animal'].isin(cats) & df['Year'].isin(years)]
pivot = pd.pivot_table(filter, index=['Animal'], columns='Year',
values=['Dollar Amount'], aggfunc=np.sum, fill_value=0)
filter2 = df[df['Roll'].isin(Analyst) & df['Animal'].isin(dogs) & df['Year'].isin(years)]
pivot2 = pd.pivot_table(filter2, index=['Animal'], columns='Year',
values=['Dollar Amount'], aggfunc=np.sum, fill_value=0)
Total = pd.concat([pivot1, pivot2])
Total = Total.append(Total.sum().rename('Analyst'))
dfs[region] = df_region
All_regions = pd.concat(['df_US', 'df_EM', 'df_Europe', 'df_Asia'])