以下代码生成名为out
的pandas表。
import pandas as pd
import numpy as np
df = pd.DataFrame({'Book': ['B1', 'B1', 'B2', 'B3', 'B3', 'B3'],
'Trader': ['T1', 'Z2', 'Z2', 'T1', 'U3', 'T2'],
'Position':[10, 33, -34, 87, 43, 99]})
df = df[['Book', 'Trader', 'Position']]
table = pd.pivot_table(df, index=['Book', 'Trader'], values=['Position'], aggfunc=np.sum)
print(table)
tab_tots = table.groupby(level='Book').sum()
tab_tots.index = [tab_tots.index, ['Total'] * len(tab_tots)]
print(tab_tots)
out = pd.concat(
[table, tab_tots]
).sort_index().append(
table.sum().rename(('Grand', 'Total'))
)
注意第二个表总是将'Total'放在底部。所以基本上我仍然想按字母顺序排序,但我想总是把'Total'放在最后。有人可以提供我的代码调整,以提供我想要的输出吗?
答案 0 :(得分:1)
Pandas在pivot_table
函数中具有内置功能来计算边际总数。
table = pd.pivot_table(df,
index='Book',
columns='Trader',
values='Position',
aggfunc=np.sum,
margins=True,
margins_name='Total').drop('Total').stack()
table[('Grand', 'Total')] = table.sum()
table.name = 'Position'
table.reset_index()
Book Trader Position
0 B1 T1 10.0
1 B1 Z2 33.0
2 B1 Total 43.0
3 B2 Z2 -34.0
4 B2 Total -34.0
5 B3 T1 87.0
6 B3 T2 99.0
7 B3 U3 43.0
8 B3 Total 229.0
13 Grand Total 238.0
此解决方案从您的out
DataFrame开始,继续您的分析。您可以将Book
和Trader
转换为Pandas分类类型,它允许您通过传递参数ordered=True
和categories
的列表来自定排序。
out = out.reset_index()
trader_cats = pd.Categorical(out['Trader'],
categories=sorted(df.Trader.unique()) + ['Total'],
ordered=True)
book_cats = pd.Categorical(out['Book'],
categories=sorted(df.Book.unique()) + ['Grand'],
ordered=True)
out['Trader'] = trader_cats
out['Book'] = book_cats
out.set_index(['Book', 'Trader'], inplace=True)
out.sort_index(level=['Book', 'Trader'])
Position
Book Trader
B1 T1 10
Z2 33
Total 43
B2 Z2 -34
Total -34
B3 T1 87
T2 99
U3 43
Total 229
Grand Total 238
答案 1 :(得分:1)
您可以使用groupby
与unstack
进行重塑。然后,轻松添加新的hostname001
列,计算Total
和stack
。最后按loc
添加新行:
Grand Total
与其他解决方案相比:
df1 = df.groupby(['Book','Trader']).Position.sum().unstack()
df1['Total'] = df1.sum(1)
all_sum = df1['Total'].sum()
df1 = df1.stack()
df1.loc[('Grand','Total')] = all_sum
df1 = df1.reset_index(name='Position')
print (df1)
Book Trader Position
0 B1 T1 10.0
1 B1 Z2 33.0
2 B1 Total 43.0
3 B2 Z2 -34.0
4 B2 Total -34.0
5 B3 T1 87.0
6 B3 T2 99.0
7 B3 U3 43.0
8 B3 Total 229.0
9 Grand Total 238.0
结论:
对于小计是更快的def jez(df):
df1 = df.groupby(['Book','Trader']).Position.sum().unstack()
df1['Total'] = df1.sum(1)
all_sum = df1['Total'].sum()
df1 = df1.stack()
df1.loc[('Grand','Total')] = all_sum
df1 = df1.reset_index(name='Position')
return (df1)
def ted1(df):
table = pd.pivot_table(df,
index=['Book'],
columns=['Trader'],
values=['Position'],
aggfunc=np.sum,
margins=True,
margins_name='total')
return table.stack()\
.rename({'total':'Total'})\
.reset_index(1)\
.rename({'Total':'Grand'})\
.reset_index()\
.query('Book != "Grand" | Trader == "Total"')
print (jez(df))
print (ted1(df))
In [419]: %timeit (jez(df))
100 loops, best of 3: 5.65 ms per loop
In [420]: %timeit (ted1(df))
10 loops, best of 3: 26.5 ms per loop
解决方案,也更简单groupby+unstack
小计。
sum
更容易使用旋转(一个函数),但使用小计+总行数进行操作会更复杂。