英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论