python如何检查dataframe中的值是否为nan

2024-04-25 06:39:32 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有方法检查数据帧的特定行和列中的特定值是否为nan?

我试过np.isnan(df.loc[no,'LatinDesc') 其中no是行no,但我得到以下错误:

输入类型不支持ufunc“isnan”,并且无法根据强制转换规则“safe”将输入安全强制转换为任何受支持的类型


Tags: 方法no类型df错误npnanloc
1条回答
网友
1楼 · 发布于 2024-04-25 06:39:32

您可以为此使用内置的pandas功能。举例说明:

import pandas as pd
import numpy as np

df = pd.DataFrame({'col1': np.random.rand(100),
              'col2': np.random.rand(100)})

# create a nan value in the 10th row of column 2
df.loc[10, 'col2'] = np.nan

pd.isnull(df.loc[10, :]) # will give true for col2

相关问题 更多 >