Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe: how to add column with number of occurrences in other column

I have to following df:

Col1    Col2
test    Something
test2   Something
test3   Something
test    Something
test2   Something
test5   Something

I want to get

Col1    Col2          Occur
test    Something     2
test2   Something     2
test3   Something     1
test    Something     2
test2   Something     2
test5   Something     1

I've tried to use:

df["Occur"] = df["Col1"].value_counts()

But it didn't help. I've got Occur column full of 'NaN'

like image 416
Laser Avatar asked May 06 '16 17:05

Laser


People also ask

How do you count occurrences of specific value in pandas column?

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

How do I add multiple columns from one DataFrame to another in pandas?

Using DataFrame. insert() method, we can add new columns at specific position of the column name sequence. Although insert takes single column name, value as input, but we can use it repeatedly to add multiple columns to the DataFrame.

How do you count the number of repeated values in pandas?

You can count the number of duplicate rows by counting True in pandas. Series obtained with duplicated() . The number of True can be counted with sum() method.


2 Answers

You can also use GroupBy + transform with size:

df['Occur'] = df.groupby('Col1')['Col1'].transform('size')

print(df)

    Col1       Col2  Occur
0   test  Something      2
1  test2  Something      2
2  test3  Something      1
3   test  Something      2
4  test2  Something      2
5  test5  Something      1
like image 101
jpp Avatar answered Oct 20 '22 16:10

jpp


groupby on 'col1' and then apply transform on Col2 to return a Series with its index aligned to the original df so you can add it as a column:

In [3]:
df['Occur'] = df.groupby('Col1')['Col2'].transform(pd.Series.value_counts)
df

Out[3]:
    Col1       Col2 Occur
0   test  Something     2
1  test2  Something     2
2  test3  Something     1
3   test  Something     2
4  test2  Something     2
5  test5  Something     1
like image 45
EdChum Avatar answered Oct 20 '22 17:10

EdChum