Search code examples
pythonpandasnonetypegeopandas

Identify pandas column that contains None value


I have a geopandas dataframe gdf that looks like the following:

   Id  text  float                           geometry
0   0  1.65   0.00  POINT (1173731.7407 5354616.9386)
1   0  None   2.20   POINT (1114084.319 5337803.2708)
2   0  2.25   6.55  POINT (1118876.2311 5307167.5724)
3   0     0   0.00  POINT (1179707.5312 5313710.8389)

How can I identify the column/s that contain a None value?

I have tried using the following list comprehension without success:

import pandas as pd
import geopandas as gp

gdf = gp.read_file('/temp/myshapefile.shp')

s = [pd.isnull(col) for col in gdf.columns if True]

Which results in:

In [1]: s
Out[1]: [False, False, False, False]

My desired output in this case is:

['text']

Solution

  • print(gdf.isna().any())
    

    will give output which column contains null in terms of true or false

    Id       False
    text     True
    float    False
    geometry False
    

    So use this

    print(gdf.columns[gdf.isna().any()].tolist())
    

    output:

    ['text']