按照接近另一列的值对数据框列进行排序

huangapple go评论83阅读模式
英文:

Sort dataframe columns value by close it to another column

问题

我需要 pandas 数据帧的 'value' 列按接近 'target' 值的程度进行排序:

+----------+---------+----------+
| 公司     |   值     |   目标    |
|----------+---------+----------+
| A        |   23.18 |  26.8879 |
| B        |   14.31 |  17.8298 |
| C        |   26.08 |  28.5479 |
| D        |   49.82 |  45.4238 |
| E        |   37.38 |  32.136  |
| F        |    3.92 |  11.7219 |
+----------+---------+----------+
英文:

I need the 'value' column of pandas dataframe to be sorted by how close it is to the 'target' value:

+----------+---------+----------+
| company  |   value |   target |
|----------+---------+----------+
| A        |   23.18 |  26.8879 |
| B        |   14.31 |  17.8298 |
| C        |   26.08 |  28.5479 |
| D        |   49.82 |  45.4238 |
| E        |   37.38 |  32.136  |
| F        |    3.92 |  11.7219 |
+----------+---------+----------+

答案1

得分: 2

使用np.argsort对两列的绝对差值进行排序:

import numpy as np
out = df.iloc[np.argsort(df['value'].sub(df['target']).abs())]

或者,使用sort_values进行链式命令:

df.sort_values(by='value', key=lambda x: x.sub(df['target']).abs())

输出:

  公司    值      目标
2  C   26.08  28.5479
1  B   14.31  17.8298
0  A   23.18  26.8879
3  D   49.82  45.4238
4  E   37.38  32.1360
5  F    3.92  11.7219
英文:

Use np.argsort on the absolute difference of the two columns:

import numpy as np
out = df.iloc[np.argsort(df['value'].sub(df['target']).abs())]

Or, as a chained command using sort_values:

df.sort_values(by='value', key=lambda x: x.sub(df['target']).abs())

Output:

  company  value   target
2       C  26.08  28.5479
1       B  14.31  17.8298
0       A  23.18  26.8879
3       D  49.82  45.4238
4       E  37.38  32.1360
5       F   3.92  11.7219

答案2

得分: 0

你也可以手动创建依赖列,并直接对其进行排序:

print(
    df.eval('absval = abs(target - value)')
    .sort_values('absval')
)

     公司   数值    目标值   绝对值
2  C    26.08  28.5479  2.4679
1  B    14.31  17.8298  3.5198
0  A    23.18  26.8879  3.7079
3  D    49.82  45.4238  4.3962
4  E    37.38  32.1360  5.2440
5  F    3.92   11.7219  7.8019
英文:

You can also manually create the dependent column and sort directly on that:

print(
    df.eval('absval = abs(target - value)')
    .sort_values('absval')
)

     company  value   target  absval
2  C          26.08  28.5479  2.4679
1  B          14.31  17.8298  3.5198
0  A          23.18  26.8879  3.7079
3  D          49.82  45.4238  4.3962
4  E          37.38  32.1360  5.2440
5  F           3.92  11.7219  7.8019

huangapple
  • 本文由 发表于 2023年3月20日 23:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定