是否有任何条件会使pandas dataframe函数rank
在其返回值中返回整数和浮点数的混合,或者输出始终保证为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
Run Code Online (Sandbox Code Playgroud)
几个等级选项
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]
Run Code Online (Sandbox Code Playgroud)
我发现上面的代码产生了一个浮点解决方案。可能是熊猫版本问题或其他一些版本问题。正如解决方案多样性的附录一样,以下也有效。
DataFrame({'A' : Series([1,2,3,4]), 'B' : Series([1,1,1,1]) }).rank(method='min').astype(int);
Run Code Online (Sandbox Code Playgroud)
这适用于人们可能在等级内使用的任何方法。
归档时间: |
|
查看次数: |
3383 次 |
最近记录: |