Given a DataFrame such as
import pandas as pd
df = pd.DataFrame({'a': [700, 700, 600, 600, 600]}
How can I extend it with a column containing the occurrence for each row? Such that it ends up as
df_ext = pd.DataFrame({
'a': [700, 700, 600, 600, 600],
'Occurrence': [ 2, 2, 3, 3, 3]
})
Adding the column is not so much of a problem, rather how to get the occurrence list.
CodePudding user response:
You can transform groupby_count:
df['Occurrences'] = df.groupby('a')['a'].transform('count')
or you can map value_counts on "a":
df['Occurrences'] = df['a'].map(df['a'].value_counts())
Output:
a Occurrences
0 700 2
1 700 2
2 600 3
3 600 3
4 600 3
CodePudding user response:
df['occurance']=df['a'].map(df.groupby('a').agg('size'))
CodePudding user response:
Simply use:
df['b'] = df['a'].apply(lambda x: list(df['a']).count(x))
