【问题标题】:Filtering null values from keys of dictionary- Python从字典的键中过滤空值 - Python
【发布时间】:2014-12-07 00:28:36
【问题描述】:

我有一个 pandas 数据框,并根据数据框的列创建了一个字典。字典几乎生成良好,但唯一的问题是我尝试过滤掉 NaN 值但我的代码不起作用,所以字典中有 NaN 作为键。我的代码如下:

for key,row in mr.iterrows():
    # With this line I try to filter out the NaN values but it doesn't work
    if pd.notnull(row['Company nameC']) and pd.notnull(row['Company nameA']) and pd.notnull(row['NEW ID'])  :
        newppmr[row['NEW ID']]=row['Company nameC']

输出是:

defaultdict(<type 'list'>, {nan: '1347 PROPERTY INS HLDGS INC', 1.0: 'AFLAC INC', 2.0: 'AGCO CORP', 3.0: 'AGL RESOURCES INC', 4.0: 'INVESCO LTD', 5.0: 'AK STEEL HOLDING CORP', 6.0: 'AMN HEALTHCARE SERVICES INC', nan: 'FOREVERGREEN WORLDWIDE CORP'

所以,我不知道如何过滤出 nan 值以及我的代码有什么问题。

编辑:

我的熊猫数据框的一个例子是:

        CUSIP           Company nameA   A�O     NEW ID  Company nameC
42020   98912M201       NaN             NaN     NaN     ZAP
42021   989063102       NaN             NaN     NaN     ZAP.COM CORP
42022   98919T100       NaN             NaN     NaN     ZAZA ENERGY CORP
42023   98876R303       NaN             NaN     NaN     ZBB ENERGY CORP

【问题讨论】:

    标签: python dictionary pandas nan


    【解决方案1】:

    粘贴示例 - 如何从字典中删除“nan”键:

    让我们用 'nan' 键(数值数组中的 NaN)创建 dict

    >>> a = float("nan")
    >>> b = float("nan")
    >>> d = {a: 1, b: 2, 'c': 3}
    >>> d
    {nan: 1, nan: 2, 'c': 3}
    

    现在,让我们删除所有 'nan' 键

    >>> from math import isnan
    >>> c = dict((k, v) for k, v in d.items() if not (type(k) == float and isnan(k)))
    >>> c
    {'c': 1}
    

    其他可以正常工作的场景。也许我错过了什么?

    In [1]: import pandas as pd
    
    In [2]: import numpy as np
    
    In [3]: df = pd.DataFrame({'a':[1,2,3,4,np.nan],'b':[np.nan,np.nan,np.nan,5,np.nan]})
    
    In [4]: df
    Out[4]: 
        a   b
    0   1 NaN
    1   2 NaN
    2   3 NaN
    3   4   5
    4 NaN NaN
    
    In [5]: for key, row in df.iterrows(): print pd.notnull(row['a'])
    True
    True
    True
    True
    False
    
    In [6]: for key, row in df.iterrows(): print pd.notnull(row['b'])
    False
    False
    False
    True
    False
    
    In [7]: x = {}
    
    In [8]: for key, row in df.iterrows():
       ....:     if pd.notnull(row['b']) and pd.notnull(row['a']):
       ....:         x[row['b']]=row['a']
       ....:         
    
    In [9]: x
    Out[9]: {5.0: 4.0}
    

    【讨论】:

    • 我明白了,但我想知道我的代码有什么问题,并在插入字典之前过滤 nan。谢谢你给我的想法!
    • 这很奇怪。我已经创建了与您类似的示例,并且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2010-11-15
    • 2015-05-17
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多