栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python Pandas:如何在数据框的列中拆分排序的字典

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python Pandas:如何在数据框的列中拆分排序的字典

另一种方法是定义一个仅调用

min
dict并返回一个Series的函数,以便您可以分配给多个列(函数主体取自@Alex
Martelli的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)dfOut[17]:     asn  id  orgs  value0   3320   0         Deutsche Telekom AG   22881  47886   1  Equinix (Netherlands) B.V.      72  47601   2  fusion services   10243  33438   3     Highwinds Network Group    893

编辑

如果您的数据包含空dics,那么您可以测试

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},{}]})dfOut[34]:     asn  id   orgs0   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)dfOut[36]:     asn  id  orgs  value0   3320   0         Deutsche Telekom AG   22881  47886   1  Equinix (Netherlands) B.V.      72  47601   2  fusion services   10243  33438   3     Highwinds Network Group    8934     56   4   NaN    NaN


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号