Reputation: 1811
My dataframe looks like this:
name1 name2 value
1 Jane Foo 2
2 Jane Bar 4
3 John Foo 7
4 John Bar 1
If I do df.groupby(['name1', 'name2']).count()
I get:
value
name1 name2
Jane Foo 1
Jane Bar 1
John Foo 1
John Bar 1
But I'm trying to find the rank of each value within each multiindex group. Ideally, if I use df.groupby(['name1', 'name2']).rank()
I should get:
value
name1 name2
Jane Foo 2
Jane Bar 1
John Foo 1
John Bar 2
But instead I simply get:
value
1 1
2 1
3 1
4 1
with the names of the grouped columns removed, only the index numbers as the index, and the rank value for all rows equaling 1. What am I doing wrong?
Upvotes: 1
Views: 482
Reputation: 862511
I think you need working with numeric - so it seems need grouping be first column name1
and return rank
for value
:
df['rank'] = df.groupby('name1')['value'].rank(method='dense', ascending=False).astype(int)
print (df)
name1 name2 value rank
1 Jane Foo 2 2
2 Jane Bar 4 1
3 John Foo 7 1
4 John Bar 1 2
Upvotes: 4