Splitting Dataframe in to sub dataframe based on non NULL values

Question:

I have a pandas dataframe as follows:

a       b       c
0   1.0     NaN     NaN
1   NaN     7.0     5.0
2   3.0     8.0     3.0
3   4.0     9.0     2.0
4   5.0     0.0     NaN

I am using below to split it in sub dataframes.

for _, x in df.groupby(df.isnull().dot(df.columns)):
      print(x.dropna(1))

Is there a way to order the sub dataframes with more number of columns first?

Asked By: DEs

||

Answers:

You could check with add one more groupby key

for _, x in df.groupby([df.isnull().sum(1),df.isnull().dot(df.columns)]):
    ...:       print(x.dropna(1))
    ...:       
     a    b    c
2  3.0  8.0  3.0
3  4.0  9.0  2.0
     b    c
1  7.0  5.0
     a    b
4  5.0  0.0
     a
0  1.0
Answered By: BENY
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.