我有一个这样的数据名:

#Load the required libraries
import pandas as pd
import matplotlib.pyplot as plt

#Create data_set
data_set = {'id': [1,
                   1,
                   1, 
                   1,
                   1,

                   2,
                   2,
                   2, 
                   2,
                   2,

                   3,
                   3,
                   3, 
                   3,
                   3,

                   ],
            'Salary': [16,
                       16,
                       16, 
                       16,
                       16,

                       42,
                       42,
                       42, 
                       42,
                       42,

                       28,
                       28,
                       28, 
                       28,
                       28,
                       ],
            'Reference_id': [1,
                             2,
                             3, 
                             4,
                             5,

                             1,
                             2,
                             3, 
                             4,
                             5,

                             1,
                             2,
                             3, 
                             4,
                             5,
                             ],

            'Reference_Salary': [10,
                                 20,
                                 30, 
                                 40,
                                 50,

                                 10,
                                 20,
                                 30, 
                                 40,
                                 50,

                                 10,
                                 20,
                                 30, 
                                 40,
                                 50,
                                 ],
             'Expenditure': [100,
                             210,
                             320,
                             430,
                             540,

                             90,
                             200,
                             310,
                             420,
                             530,

                             80,
                             190,
                             300,
                             410,
                             520,
                             ],
            
               'Leaves': [ 17.5,
                           45. ,
                           72.5,
                           100. ,
                           127.5,

                           7.5,
                           35. ,
                           62.5,
                           90. ,
                          117.5,

                           2.5,
                           25. ,
                           52.5,
                           80. ,
                           107.5,
                           ],
               }

#Convert to dataframe
df = pd.DataFrame(data_set)
print("\n df = \n",df)

数据框如下所示:

enter image description here

这里,‘id=1’出现了5次,需要与‘Reference_Salary=10到50’进行比较,后者由其对应的‘Reference_id=1到5’来标记.

薪资差异以及整行应该从最小到最大显示.

同样的逻辑也适用于df的其他id.

我希望在数据帧中看到结果:

enter image description here

有没有人能告诉我如何用Python语言来完成这个任务?

推荐答案

如果我理解正确的话,你可以用np.abs():

df['tmp'] = np.abs(df['Salary'] - df['Reference_Salary'])
df = df.sort_values(by=['id', 'tmp'])
df.pop('tmp')

print(df)

打印:

    id  Salary  Reference_id  Reference_Salary  Expenditure  Leaves
1    1      16             2                20          210    45.0
0    1      16             1                10          100    17.5
2    1      16             3                30          320    72.5
3    1      16             4                40          430   100.0
4    1      16             5                50          540   127.5
8    2      42             4                40          420    90.0
9    2      42             5                50          530   117.5
7    2      42             3                30          310    62.5
6    2      42             2                20          200    35.0
5    2      42             1                10           90     7.5
12   3      28             3                30          300    52.5
11   3      28             2                20          190    25.0
13   3      28             4                40          410    80.0
10   3      28             1                10           80     2.5
14   3      28             5                50          520   107.5

Python-3.x相关问答推荐

类型注释:pathlib. Path vs importlib. resources. abc. Traversable

Python3和请求-超文本标记语言:试图抓取一个网站-没有取回真正的超文本标记语言代码

在Python中从列创建新行

我们可以在每个可以使用 Pandas Join 的用例中使用 Pandas merge 吗?

类变量的Python子类被视为类方法

Django 模型类方法使用错误的 `self`

命名空间前缀无效

在新数据帧上自动提取两个字符串 python 之间的相等性

如何沿单列获取嵌套列表中的唯一值?

如何使用复选按钮更改 Pyplot 轴的属性?

Python多进程:运行一个类的多个实例,将所有子进程保留在内存中

通过最接近的匹配合并两个不同长度的列上的两个数据框

请求:RecursionError:超出最大递归深度

python 3:如何判断一个对象是否是一个函数?

例外:使用 Pyinstaller 时找不到 PyQt5 插件目录,尽管 PyQt5 甚至没有被使用

Python 3.5:async with导致 SyntaxError.为什么?

Python在OrderedDict中 Select 第i个元素

如何避免使用我的 python 包构建 C 库?

如何从集合中删除多个元素?

有没有办法在多个线程中使用 asyncio.Queue ?