Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

isna search on mulitple columns/rows gives error: duplicate columns & possible reduce dimensionality

I’m trying to search for NaN values in different columns and rows and want to show all rows.

My dataset:

import pandas as pd
df = pd.read_excel('./file.xlsx')
df = df.replace(' ', np.nan)
df.head()
 
                             col1    col2      col3

row1                         NaN     NaN       test
row2                         hello   twon      street
row3                         words   place     NaN
row4                         NaN     towns     places
row5                         town    NaN       NaN  

I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

nan_rows = df[df['col1', 'col2', 'col3'].isna().any(axis=1)]

This gives a key-error:

  2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

Expected result:

                             col1    col2      col3

row1                         NaN     NaN       test
row3                         words   place     NaN
row4                         NaN     towns     places
row5                         town    NaN       NaN

Appreciate the help and effort. Thank you!

>Solution :

You need double square brackets to slice with a list of columns:

df[df[['col1', 'col2', 'col3']].isna().any(axis=1)]

Alternative using a named list:

cols = ['col1', 'col2', 'col3']

df[df[cols].isna().any(axis=1)]

output:

       col1   col2    col3
row1    NaN    NaN    test
row3  words  place     NaN
row4    NaN  towns  places
row5   town    NaN     NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading