简体   繁体   中英

how to read and check in empty and nan values for python

Here's my matlab script and I want to make a similar condition which will check and print 'no such attribute' or 'is empty' in python. It will read out the values from the whole dataframe. My input will be the data read from an excel sheet.

enter image description here

function [value] = findAttValue(text,raw,attStr)

cellArray = strfind(text,attStr);
[cellRow, cellCol] = find(~cellfun(@isempty,cellArray));

if isempty(cellRow)
    value = [];
    disp([attStr ': no such Attribute'])
elseif isnan(raw{cellRow, cellCol+1})
    value = [];
    disp([attStr ' is empty'])
else
    value = raw{cellRow, cellCol+1};
end

% value = raw{cellRow, cellCol+1};

I tried this functions but were not giving me the answer.

if df=='':        #Option 1 
if pd.isnull(df): #Option 2
if np.isnan(df):  #Option 3

Also I came across a function like this but I am not sure how to use it or whether it is a correct one:

import numpy as np
mask = np.column_stack([df[col].astype(str).str.contains("data", na=False) for col in df])
df.loc[mask.any(axis=1)]

I was facing similar challenge some days back. My dataset was simple so, I first filled NA values with some string and used that to make a conditional argument. Obviously, not a great way though!

Snip of my code

key["SUPPLIER/S"].fillna("NA", inplace = True)

if key["SUPPLIER/S"][i] == "NA":
   then x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM