How to determine whether there are missing values None or NaN(nan) in the DataFrame? How to get the index subscript of DataFrame according to the value?

Assuming the following data:

x=["hello","world",None]
xx=pd.DataFrame(x)
xx

Data are as follows:
Insert picture description here

As you can see, the second row and the first column have no data and are empty None. But if our data has 100,000 rows, then we don't know if there are missing values. When there is a missing value, 99% of the follow-up processing will inadvertently report an error, leaving you jiao'er monk confused and don't know where the error was reported, so be sure to solve it as soon as possible.
The judgment method is as follows:

xx.isnull()
#或者
xx==None

The results are as follows:
Insert picture description here
you can see isnull()that a xxdataframe with the same shape as the original shape will be returned , and each value in it indicates whether the original corresponding position is None. However, when the data is more than 100,000 rows, we still may not see whether there are elements True. At this time, it can be used as in numpy any().

xx.isnull().any()

The result is as follows:
Insert picture description here
What if you want to know the Nonesubscript of the element? You can use the method in numpy.
We will xx.isnull()save it first and convert it to a numeric value of 0 or 1.

b=xx.isnull()
b[b==True]=1
b

Ie:
Insert picture description here
then

#转成numpy数组,否则argwhere会报错。
bb=np.array(b)
np.argwhere(bb==1)

The final precise subscript is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43391414/article/details/113004235