【发布时间】:2015-06-26 21:19:40
【问题描述】:
我有一个这样的数据框:
id asn orgs
0 3320 {'Deutsche Telekom AG': 2288}
1 47886 {'Joyent': 16, 'Equinix (Netherlands) B.V.': 7}
2 47601 {'fusion services': 1024, 'GCE Global Maritime':16859}
3 33438 {'Highwinds Network Group': 893}
我想对实际上是字典的“orgs”列进行排序,然后提取在两个不同列中具有最高值的 pair(k,v)。像这样:
id asn org value
0 3320 'Deutsche Telekom AG' 2288
1 47886 'Joyent' 16
2 47601 'GCE Global Maritime' 16859
3 33438 'Highwinds Network Group' 893
目前我正在运行此代码,但它没有正确排序,然后我不确定如何提取具有最高值的对。
df.orgs.apply(lambda x : sorted(x.items(),key=operator.itemgetter(1),reverse=True))
这给了我一个这样的清单:
id asn orgs
0 3320 [('Deutsche Telekom AG', 2288)]
1 47886 [('Joyent', 16),( 'Equinix (Netherlands) B.V.', 7)]
2 47601 [('GCE Global Maritime',16859),('fusion services', 1024)]
3 33438 [('Highwinds Network Group', 893)]
现在我怎样才能将最高的键和值放在两个单独的列中?有人可以帮忙吗?
【问题讨论】:
-
嗯,你要求的只是最大值,排序有点无关紧要,不是吗?
-
@EdChum 否,因为我希望将键和值都放在具有最大值的对的单独列中。
标签: python sorting dictionary pandas dataframe