I want to count for each value in the column in the dataframe, how many values are greater than or equal to that value in the column. Then i want to store this count value in a new column in the dataframe.
I think you want something like this:
df=pd.DataFrame({'values':[1,4,3,23,6,7,8,22,55,43,10,4]})
mapper=( df['values'].sort_values(ascending=False)
.reset_index(drop=True)
.reset_index()
.drop_duplicates('values',keep='last')
.set_index('values')['index'] )
df['Greater than value']=df['values'].map(mapper)
print(df)
values Greater than value
0 1 11
1 4 9
2 3 10
3 23 2
4 6 7
5 7 6
6 8 5
7 22 3
8 55 0
9 43 1
10 10 4
11 4 9
df=pd.DataFrame({'values':[1,4,3,23,6,7,8,22,55,43,10,4]})
counts = ( (df.sort_values('values',ascending=False)
.expanding().count()-1).sort_index()
.groupby(df['values'])
.transform('max') )
df=df.assign(greater_than_value=counts)
print(df)
values greater_than_value
0 1 11.0
1 4 9.0
2 3 10.0
3 23 2.0
4 6 7.0
5 7 6.0
6 8 5.0
7 22 3.0
8 55 0.0
9 43 1.0
10 10 4.0
11 4 9.0
Here transform
max is used to assign the same value to duplicates.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.