gpt4 book ai didi

python - Pandas 等级函数产生非整数?

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:34 25 4
gpt4 key购买 nike

是否有任何条件可以使 pandas dataframe 函数 rank 在其返回值中返回整数和 float 的混合,或者输出始终保证为 1...N 整数?

最佳答案

如@TomAugspurger 所示。如果它们是重复的,则它们可以是非整数。 (但无论如何都是 float64 dtype)。

In [7]: DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank()
Out[7]:
A B
0 1 2.5
1 2 2.5
2 3 2.5
3 4 2.5

[4 rows x 2 columns]

In [8]: DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank().dtypes
Out[8]:
A float64
B float64
dtype: object

多个排名选项

In [12]: DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank(method='min')
Out[12]:
A B
0 1 1
1 2 1
2 3 1
3 4 1

[4 rows x 2 columns]

In [13]: DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank(method='max')
Out[13]:
A B
0 1 4
1 2 4
2 3 4
3 4 4

[4 rows x 2 columns]

In [14]: DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank(method='first')
Out[14]:
A B
0 1 1
1 2 2
2 3 3
3 4 4

[4 rows x 2 columns]

关于python - Pandas 等级函数产生非整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com