Split pandas dataframe into multiple dataframes based on null columns

  • A+
Category:Languages

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 

Is there a simple way to split the dataframe into multiple dataframes based on non-null values?

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

 


Using groupby with dropna

for _, x in df.groupby(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 0  1.0      a    b 4  5.0  0.0 

We can save them in dict

d = {y : x.dropna(1) for y, x in df.groupby(df.isnull().dot(df.columns))} 

More Info using the dot to get the null column , if they are same we should combine them together

df.isnull().dot(df.columns) Out[1250]:  0    bc 1     a 2       3       4     c dtype: object 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: