--- title: Virgil - Intro To Pandas Seaborn - S71 Pivot Table tags: Virgil, LearnWorld, IntroPandasSeaborn --- <a target="_blank" href="https://colab.research.google.com/drive/17fdmTUJHaIRGn3jKZ0mIGfIDNHAQZOGh"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a> ## Pivot Table A pivot table is a table of statistics that summarizes the data of a more extensive table. This summary might include sums, averages, or other statistics, which the pivot table groups together in a meaningful way. OFFICIAL DOCUMENTATION ▸ https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html EXPLANATION ▸ https://pbpython.com/pandas-pivot-table-explained.html ```python df.groupby(['day', 'sex'])['total_bill'].mean() ``` day sex Fri Female 14.145556 Male 19.857000 Sat Female 19.680357 Male 20.802542 Sun Female 19.872222 Male 21.887241 Thur Female 16.715312 Male 18.714667 Name: total_bill, dtype: float64 ```python # Finding average pledged by states and main_categories pd.pivot_table(data=df, index='day', columns='sex', values='tip', aggfunc='sum') ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th>sex</th> <th>Female</th> <th>Male</th> </tr> <tr> <th>day</th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th>Fri</th> <td>25.03</td> <td>26.93</td> </tr> <tr> <th>Sat</th> <td>78.45</td> <td>181.95</td> </tr> <tr> <th>Sun</th> <td>60.61</td> <td>186.78</td> </tr> <tr> <th>Thur</th> <td>82.42</td> <td>89.41</td> </tr> </tbody> </table> </div> ```python # Perform pivot table on multiple value pd.pivot_table(data=df, index='day', columns='sex', values=['tip', 'total_bill'], aggfunc='mean') ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead tr th { text-align: left; } .dataframe thead tr:last-of-type th { text-align: right; } </style> <table border="1" class="dataframe"> <thead> <tr> <th></th> <th colspan="2" halign="left">tip</th> <th colspan="2" halign="left">total_bill</th> </tr> <tr> <th>sex</th> <th>Female</th> <th>Male</th> <th>Female</th> <th>Male</th> </tr> <tr> <th>day</th> <th></th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th>Fri</th> <td>2.781111</td> <td>2.693000</td> <td>14.145556</td> <td>19.857000</td> </tr> <tr> <th>Sat</th> <td>2.801786</td> <td>3.083898</td> <td>19.680357</td> <td>20.802542</td> </tr> <tr> <th>Sun</th> <td>3.367222</td> <td>3.220345</td> <td>19.872222</td> <td>21.887241</td> </tr> <tr> <th>Thur</th> <td>2.575625</td> <td>2.980333</td> <td>16.715312</td> <td>18.714667</td> </tr> </tbody> </table> </div> ```python # Perform pivot table on multiple value and multiple aggregation function pd.pivot_table(data=df, index='day', columns='sex', values=['tip', 'total_bill'], aggfunc='mean') ``` ```python # Perform pivot table on two set of values with different aggregation functions pd.pivot_table(data=df, index='day', columns='sex', values=['tip', 'total_bill'], aggfunc={'tip':'mean', 'total_bill':'sum'}) ```