Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: print column name with missing values

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.

like image 603
LinearLeopard Avatar asked May 21 '16 18:05

LinearLeopard


People also ask

How do you show columns with missing values in pandas?

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.

How do I print missing values in pandas?

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.

How do you check which columns have NaN values?

You can use df. isnull(). sum() . It shows all columns and the total NaNs of each feature.

How do I print column names in pandas?

To get the column names in Pandas dataframe you can type <code>print(df. columns)</code> given that your dataframe is named “df”.


2 Answers

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'] 
like image 142
ayhan Avatar answered Sep 22 '22 04:09

ayhan


Oneliner -

[col for col in df.columns if df[col].isnull().any()] 
like image 24
Vedang Mehta Avatar answered Sep 22 '22 04:09

Vedang Mehta