vmamontov

multiindex in pivot table

Jun 4th, 2022
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. import pandas as pd
  2. import random
  3.  
  4. df = pd.read_csv('https://vk.com/s/v1/doc/oL105c1nIh_2md8ugsA48yZ12YLEGVJunJAaUXqDPM5bhjK8KLA')
  5. df['target'] = [random.choice([0,1]) for _ in range(len(df))]
  6.  
  7. pivot = df.pivot_table(
  8.     values = ['training_hours'],
  9.     index = ['education_level', 'target'],
  10.     columns = ['gender'],
  11.     aggfunc = 'sum',
  12.     margins = True,
  13.     fill_value = 0,
  14. )
  15. display(pivot)
Advertisement
Add Comment
Please, Sign In to add comment