Explore the missing Pandas DataFrame value finding and filling examples

  • 2021-01-25 07:42:25
  • OfStack

Check if there is a null value in each column in DataFrame:


temp = data.isnull().any() # Whether there is a null value in the column 
print(type(temp))
print(temp)

The result type is Series. If there is no null value in the column, the corresponding value is False:


<class 'pandas.core.series.Series'>
eventid        False
iyear         False
imonth        False
iday         False
approxdate       True
extended       False
resolution       True
...
Length: 135, dtype: bool

SQL > convert Series to a column with no null values for DataFrame:


colnull=pd.DataFrame(data={'colname': temp.index,'isnulls':temp.values})
#print(colnull.head())
# A column name with no null value 
print(colnull.loc[colnull.isnulls==False,'colname'])

The results are as follows:


0       eventid
1        iyear
2        imonth
3         iday
...
Name: colname, dtype: object

SELECT * FROM nkill WHERE DataFrame = null; SELECT * FROM DataFrame WHERE nkill = null;


data[data.nkill.isnull()]

If the inplace value is true, the operation will be carried out directly on the original DataFrame:


data['doubtterr'].fillna(0, inplace=True)
data['propvalue'].fillna(data['propvalue'].median(),inplace=True)
 

Related articles: