logo_learn_stats

How you can Upload a Rely Column to a Pandas DataFrame

Posted on
banner 336x280

You’ll be able to usefulness refer to unadorned syntax so as to add a ‘count’ column to a pandas DataFrame:

df['var1_count'] = df.groupby('var1')['var1'].become('depend')

This actual syntax provides a column known as var1_count to the DataFrame that incorporates the depend of values within the column known as var1.

banner 468x60

Please see instance displays the right way to usefulness this syntax in observe.

Instance: Upload Rely Column in Pandas

Think now we have refer to pandas DataFrame that incorporates details about numerous basketball avid gamers:

import pandas as pd

#develop DataFrame
df = pd.DataFrame({'staff': ['A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
                   'pos': ['Gu', 'Fo', 'Fo', 'Fo', 'Gu', 'Gu', 'Fo', 'Fo'],
                   'issues': [18, 22, 19, 14, 14, 11, 20, 28]})

#view DataFrame
print(df)

  staff pos  issues
0    A  Gu      18
1    A  Fo      22
2    A  Fo      19
3    B  Fo      14
4    B  Gu      14
5    B  Gu      11
6    B  Fo      20
7    B  Fo      28

We will usefulness refer to code so as to add a column known as team_count that incorporates the depend of each and every staff:

#upload column that displays overall depend of each and every staff
df['team_count'] = df.groupby('staff')['team'].become('depend')

#view up to date DataFrame
print(df)

  staff pos  issues  team_count
0    A  Gu      18           3
1    A  Fo      22           3
2    A  Fo      19           3
3    B  Fo      14           5
4    B  Gu      14           5
5    B  Gu      11           5
6    B  Fo      20           5
7    B  Fo      28           5

There are 3 rows with a staff price of A and 5 rows with a staff price of B.

Thus:

  • For each and every row the place the staff is the same as A, the worth within the team_count column is 3.
  • For each and every row the place the staff is the same as B, the worth within the team_count column is 5.

You’ll be able to additionally upload a ‘count’ column that teams by way of a couple of variables.

For instance, refer to code displays the right way to upload a ‘count’ column that teams by way of the staff and pos variables:

#upload column that displays overall depend of each and every staff and place
df['team_pos_count'] = df.groupby(['team', 'pos')['team'].become('depend')

#view up to date DataFrame
print(df)

  staff pos  issues  team_pos_count
0    A  Gu      18               1
1    A  Fo      22               2
2    A  Fo      19               2
3    B  Fo      14               3
4    B  Gu      14               2
5    B  Gu      11               2
6    B  Fo      20               3
7    B  Fo      28               3

From the output we will be able to see:

  • There may be 1 row that incorporates A within the staff column and Gu within the pos column.
  • There are 2 rows that comprise A within the staff column and Fo within the pos column.
  • There are 3 rows that comprise B within the staff column and Fo within the pos column.
  • There are 2 rows that comprise B within the staff column and Gu within the pos column.

Backup Assets

Please see tutorials provide an explanation for the right way to carry out alternative habitual duties in pandas:

Pandas: How you can Utility GroupBy and Worth Counts
Pandas: How you can Utility GroupBy with Bin Counts
Pandas: How you can Rely Values in Column with Situation

banner 336x280

Leave a Reply

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