luc*_*uca 7 python pandas ranking-functions
我有一些包含一些列的DataFrame.我想添加一个新列,其中每个行值是一个现有列的分位数等级.
我可以使用DataFrame.rank对列进行排名,但后来我不知道如何获取此排名值的分位数,并将此分位数添加为新的colunm.
示例:如果这是我的DataFrame
df = pd.DataFrame(np.array([[1, 1], [2, 10], [3, 100], [4, 100]]), columns=['a', 'b'])
a b
0 1 1
1 2 10
2 3 100
3 4 100
Run Code Online (Sandbox Code Playgroud)
我想知道b列的分位数(使用2个分位数).我期待这个结果:
a b quantile
0 1 1 1
1 2 10 1
2 3 100 2
3 4 100 2
Run Code Online (Sandbox Code Playgroud)
luc*_*uca 10
我发现这很容易:
df['quantile'] = pd.qcut(df['b'], 2, labels=False)
a b quantile
0 1 1 0
1 2 10 0
2 3 100 1
3 4 100 1
Run Code Online (Sandbox Code Playgroud)
有趣的是知道" pandas.qcut和pandas.cut之间的区别 "
| 归档时间: |
|
| 查看次数: |
7037 次 |
| 最近记录: |