熊猫按条件按列值排序

Vin*_*ent 1 python sorting pandas

我有以下数据集(非唯一ID)

id   data  country
1    8     B
2    15    A
3    14    D
3    19    D
3    8     C
3    20    A
Run Code Online (Sandbox Code Playgroud)

对于国家/地区为“ A”的行,我想添加一个“等级”列。

对于国家/地区为A的行,我想将“等级”值保留为空(或0)。

预期产量:

id   data  country rank
1    8     B       1
2    15    A       0
3    14    D       3 
3    19    D       4
3    8     C       2
3    20    A       0
Run Code Online (Sandbox Code Playgroud)

这篇熊猫的专栏文章按列值提供了很好的见解。

我可以试试 :

df['rank'] = df['data'].rank(ascending=True)
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何考虑“国家”?

dra*_*ine 5

EDIT: Written before an edit to the question so doesn't do exactly what the OP wants.

df['rank_A'] = df.data[df['country']=='A'].rank(ascending=True)
Run Code Online (Sandbox Code Playgroud)

Tested on this

 import pandas as pd
 from pandas import DataFrame
 import numpy as np
 df2 = DataFrame(np.random.randn(5, 2))
 df2.columns = ['A','B']
 df2['rank'] = df2.A[df2['B']>0].rank(ascending=True)
 df2
Run Code Online (Sandbox Code Playgroud)

which gives the ranking according to A for rows in which B is greater than zero.