Python Pandas : How to split a sorted dictionary in a column of a dataframe

标签 python sorting dictionary pandas dataframe

我有一个像这样的数据框:

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”列进行排序,它实际上是一个字典,然后提取两个不同列中具有最高值的对(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)]

现在如何将键和最高值放入两个单独的列中?有人可以帮忙吗?

最佳答案

另一种方法定义一个函数,仅在字典上调用 min 并返回一个 Series,以便您可以分配给多个列(函数体取自 @Alex Martelli's answer ):

In [17]:

def func(x):
    k = min(x, key=x.get)
    return pd.Series([k, x[k]])
df[['orgs', 'value']] = df['orgs'].apply(func)
df

Out[17]:
     asn  id                        orgs  value
0   3320   0         Deutsche Telekom AG   2288
1  47886   1  Equinix (Netherlands) B.V.      7
2  47601   2             fusion services   1024
3  33438   3     Highwinds Network Group    893

编辑

如果你的数据有空的dicss,那么你可以只测试len:

In [34]:

df = pd.DataFrame({'id':[0,1,2,3,4],
                   'asn':[3320,47886,47601,33438,56],
                   'orgs':[{'Deutsche Telekom AG': 2288},
                           {'Joyent': 16, 'Equinix (Netherlands) B.V.': 7},
                           {'fusion services': 1024, 'GCE Global Maritime':16859},
                           {'Highwinds Network Group': 893},{}]})
df
Out[34]:
     asn  id                                               orgs
0   3320   0                      {'Deutsche Telekom AG': 2288}
1  47886   1    {'Equinix (Netherlands) B.V.': 7, 'Joyent': 16}
2  47601   2  {'GCE Global Maritime': 16859, 'fusion service...
3  33438   3                   {'Highwinds Network Group': 893}
4     56   4                                                 {}
In [36]:

def func(x):
    if len(x) > 0:
        k = min(x, key=x.get)
        return pd.Series([k, x[k]])
    return pd.Series([np.NaN, np.NaN])

df[['orgs', 'value']] = df['orgs'].apply(func)
df

Out[36]:
     asn  id                        orgs  value
0   3320   0         Deutsche Telekom AG   2288
1  47886   1  Equinix (Netherlands) B.V.      7
2  47601   2             fusion services   1024
3  33438   3     Highwinds Network Group    893
4     56   4                         NaN    NaN

关于Python Pandas : How to split a sorted dictionary in a column of a dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742975/

相关文章:

Scala 映射 isDefinedAt() 与 contains() 方法

python - 根据类别对列表进行分组

python - 用 Python 替换文件中的文本

javascript - 无法使用 sorttype 函数对 jqGrid 列进行排序

c++ - 在堆数据结构中环绕问题

python - 如何从字典中构造一个 defaultdict?

python - HTTP 错误 504 : Gateway Time-out when trying to read a reddit comments post

python - 如何切换位

javascript - 如何根据优先级对不同类型的数据进行排序?

java - 如何在Java中使用Map(或Set?)找到最常见的元素?