R中ntile()的熊猫等效项

时间:2019-04-17 18:41:47

标签: python r pandas numpy

我同时学习熊猫和R,想知道是否有办法 在大熊猫里做下面的事情?

y = c(3,2,2,NA,30,4)
ntile(y, n=2) # 1  1  1 NA  2  2

Pandas
y = pd.Series((3,2,2,np.nan,30,4))
??

Explanation:
From: (3,2,2,NA,30,4)
To:   1  1  1 np.nan  2  2
Logic: first three number are smaller in rank and assign rank 1
       last two values are larger so have rank 2.

**Required Output**
array([1  1 1 nan  2  2])

1 个答案:

答案 0 :(得分:2)

尝试:

thirdPartyCarId

如果您想要数字答案:

pd.qcut(y, q=2)

0    (1.999, 3.0]
1    (1.999, 3.0]
2    (1.999, 3.0]
3             NaN
4     (3.0, 30.0]
5     (3.0, 30.0]
dtype: category
Categories (2, interval[float64]): [(1.999, 3.0] < (3.0, 30.0]]