Pandas DataFrames:迭代行并检查值是否为 NULL 或“nan”

问题描述 投票:0回答:2

我正在使用 pandas 加载 Excel 文件并迭代它的行,检查“状态”列是否不为 NULL,并且我收到错误

AttributeError: 'str' object has no attribute 'isnull'
,我也尝试了
isna(), is None

代码:

data = read_excel('ExcelFile.xlsx')
results = {

}
for index, row in data.iterrows():
    if row['Status'].isnull():
        print('NULL')

数据框:

   ID   Status
0   0  Success
1   1      NaN
python python-3.x pandas dataframe
2个回答
1
投票

检查“Status”列的类型,可能是一个对象(str)。并且 str 对象没有 isnull() 方法。尝试一下:

data = read_excel('ExcelFile.xlsx')
results = {

}
for index, row in data.iterrows():
    if row['Status'] == 'NaN':
        print('NULL')

您可以使用 data.info() 检查列的类型


0
投票

使用 pd.isnull()

data = read_excel('ExcelFile.xlsx')
results = {

}
for index, row in data.iterrows():
    if pd.isnull(row['Status']):
        print('NULL')
© www.soinside.com 2019 - 2024. All rights reserved.