根据空列将pandas数据框拆分为多个数据框 [英] Split pandas dataframe into multiple dataframes based on null columns

查看:153
本文介绍了根据空列将pandas数据框拆分为多个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,如下所示:

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


推荐答案

使用 groupby 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

这篇关于根据空列将pandas数据框拆分为多个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆