Skip to content

Instantly share code, notes, and snippets.

@ecarreras
Created April 13, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ecarreras/d833fa1a6a1f736365f41b3dd453076c to your computer and use it in GitHub Desktop.
Save ecarreras/d833fa1a6a1f736365f41b3dd453076c to your computer and use it in GitHub Desktop.
Pandas pivot table
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