Home > database >  New column that counts the frequency that a value occurs in a Pandas dataframe column
New column that counts the frequency that a value occurs in a Pandas dataframe column

Time:09-28

I have a dataframe that looks like

ID   feature
1    2 
1    3
1    4
2    3
2    2
3    5
3    8
3    4
3    2
4    4
4    6

and I want to add a new column n_ID that counts the number of times that an element occur in the column ID, so the desire output should look like

ID   feature  n_ID
1    2        3
1    3        3
1    4        3
2    3        2
2    2        2
3    5        4
3    8        4
3    4        4
3    2        4
4    4        2
4    6        2

I know the .value_counts() function but I don't know how to make use of this method to make the new column. Thanks in advance

CodePudding user response:

Using value counts... I was thinking of this... @sophocles Thanks for transform... :)

df = pd.DataFrame({"ID":[1,1,1,2,2,3,3,3,3,4,4],
                    "feature":[1,2,3,4,5,6,7,8,9,10,11]})
df1 = pd.DataFrame(df["ID"].value_counts().reset_index())
df1.columns = ["ID","n_ID"]

df = df.merge(df1,how = "left",on="ID")

CodePudding user response:

Just create new column and count the occurance using lambda func:

Code:

df['n_id'] = df.apply(lambda x:  df['ID'].tolist().count(x.ID), axis=1)

Output:

   ID feature   n_id
0   1   1        3
1   1   2        3
2   1   3        3
3   2   4        2
4   2   5        2
5   3   6        4
6   3   7        4
7   3   8        4
8   3   9        4
9   4   10       2
10  4   11       2
  • Related