Reputation: 301
I want to count number of similar rows in Pandas dataframe, and add it as a new column of 'count'.
For example:
A B C D
0 1 1 1 1
1 1 1 0 1
2 1 1 1 0
3 1 1 1 0
Should result in:
A B C D count
0 1 1 1 1 1
1 1 1 0 1 1
2 1 1 1 0 2
3 1 1 1 0 2
Upvotes: 0
Views: 147
Reputation: 260420
A simple solution is to groupby
on all columns and get the group size:
df['count'] = df.groupby(list(df.columns))['A'].transform('size')
output:
A B C D count
0 1 1 1 1 1
1 1 1 0 1 1
2 1 1 1 0 2
3 1 1 1 0 2
input:
df = pd.DataFrame({'A': [1, 1, 1, 1],
'B': [1, 1, 1, 1],
'C': [1, 0, 1, 1],
'D': [1, 1, 0, 0]})
Upvotes: 1