logo_learn_stats

Pandas: The way to Team By means of Index and Carry out Calculation

Posted on
banner 336x280

You’ll be able to utility please see forms to workforce by means of a number of index columns in pandas and carry out some calculation:

Form 1: Team By means of One Index Column

banner 468x60
df.groupby('index1')['numeric_column'].max()

Form 2: Team By means of A couple of Index Columns

df.groupby(['index1', 'index2'])['numeric_column'].sum()

Form 3: Team By means of Index Column and Common Column

df.groupby(['index1', 'numeric_column1'])['numeric_column2'].nunique()

Please see examples display tips on how to utility every form with please see pandas DataFrame that has a MultiIndex:

import pandas as pd

#manufacture DataFrame
df = pd.DataFrame({'workforce': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
                   'place': ['G', 'G', 'G', 'F', 'F', 'G', 'G', 'F', 'F', 'F'],
                   'issues': [7, 7, 7, 19, 16, 9, 10, 10, 8, 8],
                   'rebounds': [8, 8, 8, 10, 11, 12, 13, 13, 15, 11]})

#all set 'workforce' column to be index column
df.set_index(['team', 'position'], inplace=True)

#view DataFrame
df

		 issues	 rebounds
workforce	place		
A	G	 7	 8
        G	 7	 8
        G	 7	 8
        F	 19	 10
        F	 16	 11
B	G	 9	 12
        G	 10	 13
        F	 10	 13
        F	 8	 15
        F	 8	 11

Form 1: Team By means of One Index Column

Please see code displays tips on how to to find the max price of the ‘points’ column, grouped by means of the ‘position’ index column:

#to find max price of 'issues' grouped by means of 'place index column
df.groupby('place')['points'].max()

place
F    19
G    10
Identify: issues, dtype: int64

Form 2: Team By means of A couple of Index Columns

Please see code displays tips on how to to find the sum of the ‘points’ column, grouped by means of the ‘team’ and ‘position’ index columns:

#to find max price of 'issues' grouped by means of 'place index column
df.groupby(['team', 'position'])['points'].sum()

workforce  place
A     F           35
      G           21
B     F           26
      G           19
Identify: issues, dtype: int64

Form 3: Team By means of Index Column & Common Column

Please see code displays tips on how to to find the collection of distinctive values within the ‘rebounds’ column, grouped by means of the index column ‘team’ and the usual column ‘points’:

#to find max price of 'issues' grouped by means of 'place index column
df.groupby(['team', 'points'])['rebounds'].nunique()

workforce  issues
A     7         1
      16        1
      19        1
B     8         2
      9         1
      10        1
Identify: rebounds, dtype: int64

Spare Sources

Please see tutorials give an explanation for tips on how to carry out alternative ordinary operations in pandas:

The way to Rely Distinctive Values in Pandas
The way to Flatten MultiIndex in Pandas
The way to Exchange One or Extra Index Values in Pandas
The way to Reset an Index in Pandas

banner 336x280

Leave a Reply

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