Python等级:给负数负数

发布于 2025-01-27 06:30:35 字数 342 浏览 2 评论 0原文

我有一组基本的数据集:

ID Value
A  0.1
B  0.2
C  -0.1
D  -0.01
E  0.15

如果我们使用data.rank(),我们会得到结果:

ID Value
A  3
B  5
C  1
D  2
E  4

但是我想拥有的,以使负值导致负等级,例如:

ID Value
A  1
B  3
C  -2
D  -1
E  2

基本等级给出负值一个负等级虽然后期值获得了正等级,但我们获得了1到5,我们得到1到3和-1至-2。 任何帮助都大大关注。

I have a basic set of data like:

ID Value
A  0.1
B  0.2
C  -0.1
D  -0.01
E  0.15

If we use data.rank() we get the result:

ID Value
A  3
B  5
C  1
D  2
E  4

But i want to have so that the negative values result in an negative rank instead such as:

ID Value
A  1
B  3
C  -2
D  -1
E  2

Basically rank give the negative values an negative rank while the postive values get a positive rank but instead of 1 to 5 we get 1 to 3 and -1 to -2.
Any help is greatly apreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘落散花 2025-02-03 06:30:35

另一种类似于Concat答案的方法,但不是像Compact:

import pandas as pd

A = ['A', 'B', 'C', 'D']
B = [-1, 1, 3, -2]

df = pd.DataFrame({'ID': A, 'value': B})

pos = df.where(df['value'] >= 0)['value'].rank()
neg = df.where(df['value'] < 0)['value'].rank(ascending=False)
pos.update(-neg)

df['rank'] = pos
print(df)

输出:

  ID  value  rank
0  A     -1  -1.0
1  B      1   1.0
2  C      3   2.0
3  D     -2  -2.0

编辑:我注意到负等级未遵循您在帖子中指定的顺序,因此我在Rank()中添加了“ Aspending = false”以解决这一点。

Another method similar to the concat answer, but not as compact:

import pandas as pd

A = ['A', 'B', 'C', 'D']
B = [-1, 1, 3, -2]

df = pd.DataFrame({'ID': A, 'value': B})

pos = df.where(df['value'] >= 0)['value'].rank()
neg = df.where(df['value'] < 0)['value'].rank(ascending=False)
pos.update(-neg)

df['rank'] = pos
print(df)

Output:

  ID  value  rank
0  A     -1  -1.0
1  B      1   1.0
2  C      3   2.0
3  D     -2  -2.0

Edit: I noticed that the negative rank did not follow the order you specified in your post, so I added "ascending=False" in rank() to address that.

じее 2025-02-03 06:30:35

分别对您的正值和负值排名,然后concat它们:

>>> pd.concat([df[df["Value"].gt(0)].rank(),df[df["Value"].lt(0)].mul(-1).rank().mul(-1)]).sort_index()

    ID  Value
0  1.0    1.0
1  2.0    3.0
2 -1.5   -2.0
3 -1.5   -1.0
4  3.0    2.0

Rank your positive values and negative values separately and then concat them:

>>> pd.concat([df[df["Value"].gt(0)].rank(),df[df["Value"].lt(0)].mul(-1).rank().mul(-1)]).sort_index()

    ID  Value
0  1.0    1.0
1  2.0    3.0
2 -1.5   -2.0
3 -1.5   -1.0
4  3.0    2.0
白衬杉格子梦 2025-02-03 06:30:35

在框外思考&amp;对您的值进行排序。

# create your dummy data
data = pd.DataFrame({'ID':list('ABCDE'), 'Value':[0.1,0.2,-0.1,-0.01,0.15]})

# sort the data so that we can use of the cumsum (and vectorize the operation)
data = data.sort_values('Value')

# Cumulartive sum of the booleans for the  positive values
# Minus the the inverse sum of the booleans of the negative values
data['RANK'] = (data['Value']>=0).cumsum() - (data['Value']<0)[::-1].cumsum() 

# Sort the values back to the original state ...    
data.sort_values('ID')


     ID     Value   RANK
0     A      0.10    1
1     B      0.20    3
2     C     -0.10   -2
3     D     -0.01   -1
4     E      0.15    2

Think outside the box & sort your values.

# create your dummy data
data = pd.DataFrame({'ID':list('ABCDE'), 'Value':[0.1,0.2,-0.1,-0.01,0.15]})

# sort the data so that we can use of the cumsum (and vectorize the operation)
data = data.sort_values('Value')

# Cumulartive sum of the booleans for the  positive values
# Minus the the inverse sum of the booleans of the negative values
data['RANK'] = (data['Value']>=0).cumsum() - (data['Value']<0)[::-1].cumsum() 

# Sort the values back to the original state ...    
data.sort_values('ID')


     ID     Value   RANK
0     A      0.10    1
1     B      0.20    3
2     C     -0.10   -2
3     D     -0.01   -1
4     E      0.15    2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文