Python Pandas:使用Pandas从Groupby找到的最大值开始,使用相应的列创建新的df

时间:2018-10-05 03:50:00

标签: python pandas dataframe

使用Python熊猫,我正在尝试按“客户ID”分组找到“保存投影at50”列的最大值,并且需要获取相应的部门代码。

df的格式如下:

Customer ID                       "Annual Savings"   "Total Income" Dept Code   "Save Projection at 50"
0002806a094b4eccae0e9d8dbba3c875    0                     76,000          7000       625,862
0002806a094b4eccae0e9d8dbba3c875    0                     76,000          7008       627,862
0014e82d5c4c49d8ad2b26a8241c22cf    7,500                 36,000          7000       194,201
0014e82d5c4c49d8ad2b26a8241c22cf    7,500                 36,000          7001       195,245
001f0c417537431faac1a098eb8d0563    100,000            200,000        7000     4,309,103
001f0c417537431faac1a098eb8d0563    100,000            200,000        7001     4,525,926
001f0c417537431faac1a098eb8d0563    100,000            200,000        7005     3,629,698
0021a55561c44e4d8c240d80457fc882    234,216            150,000        7001    10,821,979
0021a55561c44e4d8c240d80457fc882    234,216            150,000        7002    10,510,446
0021a55561c44e4d8c240d80457fc882    234,216            150,000        7003     9,705,334
0021a55561c44e4d8c240d80457fc882    234,216            150,000        7004     9,529,463

NEW df应具有相应的最大值(“客户ID”和“部门代码”),其最大值分别来自““将投影保存为50””

Customer ID                             Dept Code     "Save Projection at 50"
0002806a094b4eccae0e9d8dbba3c875        7008           627,862
0014e82d5c4c49d8ad2b26a8241c22cf          7001             195,245
001f0c417537431faac1a098eb8d0563        7001         4,525,926
0021a55561c44e4d8c240d80457fc882          7001        10,821,979

有人可以帮我吗?

此刻,我只能通过以下方式获得“客户ID”和“ Save Projection at50”的最大值:

打印(data.groupby(['GUID'])['“保存投影到50”']。max())

**Customer ID                             "Save Projection at 50"**
0002806a094b4eccae0e9d8dbba3c875           627,862
0014e82d5c4c49d8ad2b26a8241c22cf           195,245
001f0c417537431faac1a098eb8d0563         4,525,926
0021a55561c44e4d8c240d80457fc882        10,821,979

0 个答案:

没有答案