【问题标题】:Doesn't pandas recognize the np.nan value? [duplicate]熊猫不识别 np.nan 值吗? [复制]
【发布时间】:2021-04-17 22:26:32
【问题描述】:

我试图检查代码中的变量是否等于 np.nan(最后一行第 4 行)。但不知何故,我在执行此操作时没有得到所需的输出,而是当我使用带有检查条件的 np.nan 时,它有点忽略了语句本身,代码似乎对于其他值运行良好。

我的代码:

import pandas as pd
import numpy as np
d={}
f=0
data=pd.read_excel('Book1.xlsx')
for i in data[:]:
    d[i]=0
    f=0
    for j in data[i]:
        print(j)
        if j==np.nan:
            f=f+1
            d[i]=f
print(d)

请告诉我是否做错了什么,以及如何解决这个问题?

【问题讨论】:

  • 这能回答你的问题吗? How can I check for NaN values?
  • @LMc 当我使用它时,我收到一个类型错误,提示 ufunc 'isnan' not supported for the input that could not be safely coerced to any supported types based on the cast rule ' ' safe '
  • 那就试试pd.isnull
  • @LMc 仍然无法正常工作。输出与我使用 np.nan 时相同。
  • 你能发布一些复制问题的示例数据吗?

标签: python pandas


【解决方案1】:

if j==np.nan:这一行改为if np.isnan(j):

【讨论】:

    【解决方案2】:

    代替

    if j==np.nan:
    

    使用

    if np.isnan(j):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2016-11-26
      • 2021-10-16
      • 2023-03-15
      • 2015-03-24
      • 2020-08-13
      • 2019-05-03
      相关资源
      最近更新 更多