Pivot Tables — Python
Published in
Jan 9, 2019
I am going to start by importing our .csv file of more trials. Assuming we have imported trials previously.
import pandas as pdmore_trials = pd.read_csv('trials_03.csv')print(more_trials)
- Now we can rearrange by pivoting:
more_trials.pivot(index='treatment', columns='gender', values='response')
- this produces an error bc there are multiple indexes…
more_trials.pivot_table(index='treatment', columns='gender', values='response')
- Could have also utilized the key word: aggfunc
more_trials.pivot_table(index='treatment', columns='gender', values='response', aggfunc='count')
Why aren’t my aggfunc’s showing???
Must add margins=True to your .pivot_table call to show the aggfunc you are using:
signups_and_visitors = users.pivot_table(index='weekday', aggfunc=sum, margins=True)
- Doing this creates a final row indexed ‘All’ with the sum of each column.