-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
pandas.core.groupby.DataFrameGroupBy.rank() dense_rank does not work #38972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @UlionTse, thanks for your report. Can you please provide a minimal reproducible example (copy-pastable!). See https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports. Especially useful for diagnosing your issue would be the expected output. |
@mzeitlin11 import pandas as pd
print(pd.__version__)
df = pd.DataFrame({'a':[1,1,1,2,2,2,3,3,3], 'b':[6,5,4,4,6,5,3,3,3]})
df['rk_min']= df.groupby(by=['a'])['b'].rank(ascending=True, method='min', na_option='bottom')
df['rk_dense']= df.groupby(by=['a'])['b'].rank(ascending=True, method='dense', na_option='bottom')
print(df) Output: '1.2.0'
a b rk_min rk_dense
0 1 6 3.0 3.0
1 1 5 2.0 2.0
2 1 4 1.0 1.0
3 2 4 1.0 1.0
4 2 6 3.0 3.0
5 2 5 2.0 2.0
6 3 3 1.0 1.0
7 3 3 1.0 1.0
8 3 3 1.0 1.0 Expected: a b rk_min rk_dense
0 1 6 3.0 3.0
1 1 5 2.0 2.0
2 1 4 1.0 1.0
3 2 4 1.0 1.0
4 2 6 3.0 3.0
5 2 5 2.0 2.0
6 3 3 1.0 1.0
7 3 3 1.0 2.0
8 3 3 1.0 3.0
'''
Parameters
method{‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}, default ‘average’
min: lowest rank in group.
dense: like ‘min’, but rank always increases by 1 between groups.
''' |
Thanks for the response @UlionTse. I believe this is expected behavior - rank always increasing by 1 between groups is referring to the following difference between 'dense' and 'min' (using an example with
'dense' ranking increases from 1 to 2 on values change (increase by 1 between "groups"), rather than 'min' which increases from 1 to 4 since there are 3 smaller values. I think the description for 'dense' ranking could certainly be clearer, however, and is especially unfortunate for the |
@mzeitlin11 Thanks. However, the dense rank after grouping is not often used. In most cases, sql: |
Does |
@mzeitlin11 OK. Thanks. |
Hi - is this issue still open? |
Yep! If you'd be interested in addressing it, the idea is to just use a clearer description for what |
Hi @mzeitlin11 , I would like to work in this. I am new to open source contribution, and might require some guidance. |
take |
Sounds great, feel free to ask any questions! |
* DOC: Adding examples to DataFrameGroupBy.rank #38972 * DOC: Adding examples to DataFrameGroupBy.rank #38972 * DOC: made the suggested changes * DOC: changed as suggested * DOC: changed as suggested * DOC: updating with black output * DOC: updating with black output * DOC: corrected docstring order
The text was updated successfully, but these errors were encountered: