Home > OS >  Splitting Dataframe in to sub dataframe based on non NULL values
Splitting Dataframe in to sub dataframe based on non NULL values

Time:10-10

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?

CodePudding user response:

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
  • Related