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

how to remove a column in dataframe if the specified column values are NaN

This is my dataframe. I need to delete the row with all the values in d1,d2,d3,c1,c2,c3 are Nan.

     no     d1      d2      d3      c1      c2      c3
0   59890   28.4    32.2    31.3    40.7    40.0    39.6
1   55679   NaN     32.8    31.5    37.3    39.2    39.4
2   58900   NaN      NaN    NaN     NaN      NaN    NaN
3   76522   34.0    32.4    32.6    45.4    NaN     46.9
4   89525   32.7    31.9    32.0    44.1    44.4    46.1
... ... ... ... ... ... ... ...

The expected output:
     no     d1      d2      d3      c1      c2      c3
0   59890   28.4    32.2    31.3    40.7    40.0    39.6
1   55679   NaN     32.8    31.5    37.3    39.2    39.4
3   76522   34.0    32.4    32.6    45.4    NaN     46.9
4   89525   32.7    31.9    32.0    44.1    44.4    46.1
... ... ... ... ... ... ... ...

>Solution :

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

You can use dropna() parameters:

df = df.dropna(subset=['d1','d2','d3','c1','c2','c3'], how='all')

Alternatively, if it’s the first column you don’t want to include:

df = df.dropna(subset=df.columns[1:], how='all')
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