【问题标题】:Pandas: If a value in a field is None/null/NaN, append the name of the field to a new fieldPandas:如果字段中的值为 None/null/NaN,则将该字段的名称附加到新字段
【发布时间】:2020-03-20 16:45:10
【问题描述】:

我对如何解决特定问题感到困惑。基本上,我希望做到以下几点:

使用 pandas,我想遍历行,如果字段中的值为 None/NaN,则将字段名称附加到新字段,如下所示。

+----+--------+----------+--------+--------+--------+---------------------------------+
| ID | Animal | Building | Letter | Fruit  | Number |           NullFields            |
+----+--------+----------+--------+--------+--------+---------------------------------+
|  1 | Dog    | House    | C      | null   | 4      | Fruit                           |
|  2 | null   | House    | null   | Apple  | null   | Animal, Letter, Number          |
|  3 | Cat    | null     | B      | Orange | null   | Building, Number                |
|  4 | null   | null     | null   | null   | 6      | Animal, Building, Letter, Fruit |
|  5 | Snake  | null     | A      | null   | 7      | Building, Fruit                 |
+----+--------+----------+--------+--------+--------+---------------------------------+

为了便于阅读,我在上面输入了“null”。我知道 None/NaN 不一样,但我正在处理的数据似乎两者都有。如果我必须运行fillna,那很好。

我认为np.where 不会在这里工作,除非我遗漏了什么。我不知道我是否需要改用iterrows 或什么。

任何提示/指导将不胜感激!

【问题讨论】:

  • 那些None/null/NaNNone 值还是相应的字符串值?
  • 无。所以无论值基本上是空的。所以你在上面看到“null”的地方什么都没有,不是字符串的值。
  • 我想到的一件事(但会很乱)是做这样的事情:df['Test1'] = np.where(((df['Animal'].isnull()) 'Name of Field', None) \n df['Test2'] = np.where(((df['Building'].isnull()) 'Name of Field', None) 为每一列,然后添加结果......这个似乎不是一种非常有效的方法,实际上我有 20 个字段可供查看。

标签: python pandas null


【解决方案1】:

这样可以:

# if ID is index, then just `df` instead of `df.iloc[...]
s = df.iloc[:,1:].isna()
df['NullFields'] = (s @ (s.columns + (', '))).str.strip(', ')

输出:

   ID Animal Building Letter   Fruit  Number                       NullFields
0   1    Dog    House      C     NaN     4.0                            Fruit
1   2    NaN    House    NaN   Apple     NaN           Animal, Letter, Number
2   3    Cat      NaN      B  Orange     NaN                 Building, Number
3   4    NaN      NaN    NaN     NaN     6.0  Animal, Building, Letter, Fruit
4   5  Snake      NaN      A     NaN     7.0                  Building, Fruit

【讨论】:

    【解决方案2】:

    首先需要创建真正的NaN 字段来衡量它们是否为空,然后我们可以使用isnull,然后使用.dot

    df['NullableFields'] = df.replace("null", np.nan).isnull().dot(df.columns)
    
    print(df)
    
        ID   Animal   Building   Letter   Fruit    Number   \
    0     1      Dog      House        C     null        4   
    1     2     null      House     null    Apple     null   
    2     3      Cat       null        B   Orange     null   
    3     4     null       null     null     null        6   
    4     5    Snake       null        A     null        7   
    
                 NullFields                                 NullableFields  
    0                            Fruit                             Fruit    
    1           Animal, Letter, Number             Animal  Letter  Number   
    2                 Building, Number                   Building  Number   
    3  Animal, Building, Letter, Fruit   Animal  Building  Letter  Fruit    
    4                  Building, Fruit                   Building  Fruit  
    

    【讨论】:

    • 谢谢!总是很高兴知道解决问题的方法不止一种!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2013-03-04
    • 2020-04-10
    相关资源
    最近更新 更多