0

I just could not figure this one out:

df.dropna(axis = 1, how="all").dropna(axis= 0 ,how="all")

All headers have data. How can I exclude the headers form a df.dropna(how="all") command. I am afraid this is going to be trivial, but help me out guys.

Thanks, Levi

sophocles
  • 13,593
  • 3
  • 14
  • 33
Levente
  • 1
  • 1
  • Can you please clarify your question further? Are you trying to drop columns that contain only NaN, or columns with more than some x Nan values? Do you also want to drop rows with NaN values or only rows with more than y NaN values? – itprorh66 Jan 22 '21 at 15:22
  • Hi there, thanks for the clarifying question. I would like to drop rows that have NaN ONLY. Further columns that have NaN values ONLY. In other words, if there is one value other than NaN then the row/ column remains. BUT every column has a header and I don't want the headers to be taken into account. A Column can be deleted despite the header if all else is NaN. Hope this adds clarity to my question. Thank you. – Levente Jan 22 '21 at 20:57

1 Answers1

0

Okay, as I understand what you want is as follows:

  • drop any column where all rows contain NaN
  • drop any row in which one or more NaN appear

So for example, given a dataframe df like:

    Id  Col1    Col2    Col3    Col4
0   1   25.0    A   NaN 6
1   2   15.0    B   NaN 7
2   3   23.0    C   NaN 8
3   4   5.0 D   NaN 9
4   5   NaN E   NaN 10

convert the dataframe by:

df.dropna(axis = 1, how="all", inplace= True)
df.dropna(axis = 0, how='all', inplace= True)

which yields:

    Id  Col1    Col2    Col4
0   1   25.0    A   6
1   2   15.0    B   7
2   3   23.0    C   8
3   4   5.0     D   9
4   5   NaN     E   10
itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • Hi, thank you for the solution you have submitted. I appreciate your time. A short correction: Drop any row that has NaN values only. (One value "saves" the column and the row. That one value cannot be the column header ) – Levente Jan 23 '21 at 13:24
  • @Levente, just updated the results to correctly show the yield from my solution. – itprorh66 Jan 23 '21 at 21:46
  • Hi @itprorh66 thank you for the solution. I upvoted your input but it does not show due to my low rep. Thanks a lot. – Levente Jan 24 '21 at 09:09
  • No problem, glad I could help – itprorh66 Jan 24 '21 at 13:48