【问题标题】:How to split a dataframe into two dataframes如何将一个数据框拆分为两个数据框
【发布时间】:2020-10-24 07:01:23
【问题描述】:

我有一个如下所示的数据框。我想将此数据框拆分为两个数据框,因为它们是不同的报告。我想拆分'break'索引。

                                            Fri      Sat      Sun      Mon      Tue
Metric Group    Metric Type
Productive Time % Available                 82.7%     88.9%   85.0%   82.8%    65.2%   
Labor Move      % Hours Lost                72.6%     70.9%   84.0%   49.0%    75.4%  
Break           % Failed Break              85.0%     50.5%   25.0%   72.9%    65.0%  
Productive Time % Available                 52.8%     90.9%   65.0%   56.9%    45.2%        
Labor Move      % Hours Lost                62.5%     80.9%   55.0%   65.9%    95.7%  
Break           % Failed Break              83.7%     85.9%   95.0%   71.9%    45.5%  

输出应该看起来像。是否有可能实现这种类型的输出。感谢任何帮助

df1 =                                              Fri      Sat      Sun      Mon      Tue
       Metric Group    Metric Type
       Productive Time % Available                 82.7%     88.9%   85.0%   82.8%    65.2%   
       Labor Move      % Hours Lost                72.6%     70.9%   84.0%   49.0%    75.4%  
       Break           % Failed Break              85.0%     50.5%   25.0%   72.9%    65.0%

df2= 

     Productive Time % Available                 52.8%     90.9%   65.0%   56.9%    45.2%        
     Labor Move      % Hours Lost                62.5%     80.9%   55.0%   65.9%    95.7%  
     Break           % Failed Break              83.7%     85.9%   95.0%   71.9%    45.5%   

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以对索引使用cumcount 创建条件列。

    这会计算我们将用来创建密钥的每个唯一变量。

    dfs = {g : data for g,data in df.groupby(df.groupby(level=0).cumcount())}
    

    print(dfs)
    
    {0:                                            Fri    Sat    Sun    Mon    Tue
     Metric Group      Metric Type                                             
     Productive Time %  Available             82.7%  88.9%  85.0%  82.8%  65.2%
     Labor Move      %  Hours Lost            72.6%  70.9%  84.0%  49.0%  75.4%
     Break           %  Failed Break          85.0%  50.5%  25.0%  72.9%  65.0%,
     1:                                            Fri    Sat    Sun    Mon    Tue
     Metric Group      Metric Type                                             
     Productive Time %  Available             52.8%  90.9%  65.0%  56.9%  45.2%
     Labor Move      %  Hours Lost            62.5%  80.9%  55.0%  65.9%  95.7%
     Break           %  Failed Break          83.7%  85.9%  95.0%  71.9%  45.5%}
    

    print(dfs[0])
    
                                               Fri    Sat    Sun    Mon    Tue
    Metric Group      Metric Type                                             
    Productive Time %  Available             82.7%  88.9%  85.0%  82.8%  65.2%
    Labor Move      %  Hours Lost            72.6%  70.9%  84.0%  49.0%  75.4%
    Break           %  Failed Break          85.0%  50.5%  25.0%  72.9%  65.0%
    

    【讨论】:

    • @Learner 一步一步遍历代码,df.groupby(level=0) 是您的索引(第一个)read about cumcount() here,最后一部分是简单的字典理解。
    【解决方案2】:

    你试过了吗:

    df1 = df.iloc(:2,:)
    df2 = df.iloc(2:,:)
    

    ?

    【讨论】:

    • 这适用于虚拟数据集,但如果存在未声明数量的变量,则无法扩展。
    • 你是什么意思? : 明确处理任意数量的变量
    • 如果有另外三行 Productive Group, Labour & Break
    • OP 提供的代码非常具体,所以我认为他不需要那个,但请确保您的答案扩大了
    【解决方案3】:

    做一个df.index.get_loc('splitword')来获取分割词的索引位置。

    splitindex=df.index.get_loc('Break')
    

    然后执行切片:

    df1=df.iloc[:(splitindex+1)]
    
    df2=df.iloc[(splitindex+1):]
    

    【讨论】:

      【解决方案4】:

      试试这个:

      ix = df.index
      break_locations = [i for i, x in enumerate(ix) if x=='Break']
      break_locations = [0]+break_locations+[len(df)]
      df_list = []
      for i in range(len(break_locations)-1):
          bl1 = break_locations[i] if  break_locations[i]==0 else  break_locations[i] +1
          bl2 = break_locations[i+1]+1
          split = df[bl1:bl2]
          if not split.empty:
              df_list.append(df[bl1:bl2])
      

      这个 sn-p 的作用是,

      • 获取索引,
      • 找出'Break' 在索引中的位置,
      • 将数据帧的开始 (0) 和结束 (len(df)) 添加到该断点列表中,
      • 然后,用两个连续的断点分割数据帧,
      • 将每个拆分存储在一个数组中df_list

      所以对于看起来像这样的数据框df

      print(df)
      
                       Fri  Sat  Sun
      Metric Group     NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      Metric Group     NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      

      你会得到一个数据框列表,如下所示:

      for dd in df_list:
          print(dd, end="\n\n")
      
                       Fri  Sat  Sun
      Metric Group     NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      
                       Fri  Sat  Sun
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      
                       Fri  Sat  Sun
      Metric Group     NaN  NaN  NaN
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      
                       Fri  Sat  Sun
      Productive Time  NaN  NaN  NaN
      Labor Move       NaN  NaN  NaN
      Break            NaN  NaN  NaN
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2016-09-28
        • 2017-05-28
        • 1970-01-01
        • 1970-01-01
        • 2020-01-08
        • 1970-01-01
        相关资源
        最近更新 更多