logo_learn_stats

Pandas: Find out how to Kind Pivot Desk through Values in Column

Posted on
banner 336x280

You’ll be able to worth please see ordinary syntax to type a pandas pivot desk in line with the values in a column:

my_pivot_table.sort_values(through=['some_column'], ascending=Fake)

This actual instance types the values in a pivot desk referred to as my_pivot_table in line with the values in some_column in descending layout.

banner 468x60

Refer to instance presentations easy methods to worth this syntax in observe.

Instance: Kind Pandas Pivot Desk through Values in Column

Assume now we have please see pandas DataFrame that accommodates details about diverse basketball gamers:

import pandas as pd

#develop DataFrame
df = pd.DataFrame({'staff': ['A', 'A', 'A', 'A', 'B', 'B',
                            'B', 'B', 'C', 'C', 'C', 'C'],
                   'issues': [4, 4, 2, 8, 9, 5, 5, 7, 8, 8, 4, 3],
                   'assists': [2, 2, 5, 5, 4, 7, 5, 3, 9, 8, 4, 4]})

#view DataFrame
print(df)

   staff  issues  assists
0     A       4        2
1     A       4        2
2     A       2        5
3     A       8        5
4     B       9        4
5     B       5        7
6     B       5        5
7     B       7        3
8     C       8        9
9     C       8        8
10    C       4        4
11    C       3        4

We will worth please see code to develop a pivot desk in pandas that presentations the sum of the values within the issues and assists columns for each and every staff:

#develop pivot desk
df_pivot = df.pivot_table(index=['team'], values=['points', 'assists'], aggfunc="sum")

#view pivot desk
print(df_pivot)

      assists  issues
staff                 
A          14      18
B          19      26
C          25      23

By means of default, pandas types the rows of the pivot desk in alphabetical layout in line with the price within the index column, which occurs to be the staff column.

Alternatively, we will be able to worth the sort_values() serve as to rather type the rows of the pivot desk in line with the values within the issues column:

#type pivot desk through price in 'issues' column in descending layout
sorted_df_pivot = df_pivot.sort_values(through=['points'], ascending=Fake)

#view looked after pivot desk
print(sorted_df_pivot)

      assists  issues
staff                 
B          19      26
C          25      23
A          14      18

Realize that the rows of the pivot desk are actually looked after in line with the values within the issues column.

Notice that in case you reduce off the ascending=Fake argument, the rows will probably be looked after through the values within the issues column in ascending layout rather:

#type pivot desk through price in 'issues' column in ascending layout
sorted_df_pivot = df_pivot.sort_values(through=['points'])

#view looked after pivot desk
print(sorted_df_pivot)

      assists  issues
staff                 
A          14      18
C          25      23
B          19      26

Realize that the rows within the pivot desk are actually looked after through the values within the issues column in ascending (smallest to greatest) layout.

Notice #1: You’ll be able to additionally type through more than one columns within the pivot desk through passing more than one values to the through argument inside the sort_values() serve as.

Notice #2: You’ll be able to in finding the entire documentation for the pandas pivot_table() serve as right here.

Backup Assets

Refer to tutorials give an explanation for easy methods to carry out alternative familiar operations in pandas:

Pandas: Find out how to Reshape DataFrame from Lengthy to Broad
Pandas: Find out how to Reshape DataFrame from Broad to Lengthy
Pandas: Find out how to Workforce and Mixture through More than one Columns

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *