Created
April 13, 2018 13:43
-
-
Save ecarreras/d833fa1a6a1f736365f41b3dd453076c to your computer and use it in GitHub Desktop.
Pandas pivot table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
%matplotlib inline | |
df = pd.read_excel('/home/ecarreras/Desktop/l.xls') | |
table = pd.pivot_table( | |
df, index=['empresa', 'producto'], values=['price_subtotal'], aggfunc=[np.sum], fill_value=0 | |
) | |
table_tots = table.groupby(level='empresa').sum() | |
table_tots.index = [table_tots.index, ['Total'] * len(table_tots)] | |
result = pd.concat([table, table_tots]).sort_index().append(table.sum().rename(('Total empresas', ''))) | |
result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment