I am trying to print or to get list of columns name with missing values. E.g.
data1 data2 data3 1 3 3 2 NaN 5 3 4 NaN
I want to get ['data2', 'data3']. I wrote following code:
print('\n'.join(map( lambda x : str(x[1]) ,(filter(lambda z: z[0] != False, zip(train.isnull().any(axis=0), train.columns.values))) )))
It works well, but I think should be simpler way.
You can extract rows/columns containing missing values from pandas. DataFrame by using the isnull() or isna() method that checks if an element is a missing value.
In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.
You can use df. isnull(). sum() . It shows all columns and the total NaNs of each feature.
To get the column names in Pandas dataframe you can type <code>print(df. columns)</code> given that your dataframe is named “df”.
df.isnull().any()
generates a boolean array (True if the column has a missing value, False otherwise). You can use it to index into df.columns
:
df.columns[df.isnull().any()]
will return a list of the columns which have missing values.
df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 2, np.nan], 'C': [4, 5, 6], 'D': [np.nan, np.nan, np.nan]}) df Out: A B C D 0 1 1.0 4 NaN 1 2 2.0 5 NaN 2 3 NaN 6 NaN df.columns[df.isnull().any()] Out: Index(['B', 'D'], dtype='object') df.columns[df.isnull().any()].tolist() # to get a list instead of an Index object Out: ['B', 'D']
Oneliner -
[col for col in df.columns if df[col].isnull().any()]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With