Pandas Split Dataframe into two Dataframes at a specific row

后端 未结 3 640
既然无缘
既然无缘 2020-12-02 16:27

I have pandas DataFrame which I have composed from concat. One row consists of 96 values, I would like to split the DataFrame from the value 72.

相关标签:
3条回答
  • 2020-12-02 16:58

    I generally use array split because it's easier simple syntax and scales better with more than 2 partitions.

    import numpy as np
    partitions = 2
    dfs = np.array_split(df, partitions)
    

    np.split(df, [100,200,300], axis=0] wants explicit index numbers which may or may not be desirable.

    0 讨论(0)
  • 2020-12-02 17:00

    iloc

    df1 = datasX.iloc[:, :72]
    df2 = datasX.iloc[:, 72:]
    

    (iloc docs)

    0 讨论(0)
  • 2020-12-02 17:19

    use np.split(..., axis=1):

    Demo:

    In [255]: df = pd.DataFrame(np.random.rand(5, 6), columns=list('abcdef'))
    
    In [256]: df
    Out[256]:
              a         b         c         d         e         f
    0  0.823638  0.767999  0.460358  0.034578  0.592420  0.776803
    1  0.344320  0.754412  0.274944  0.545039  0.031752  0.784564
    2  0.238826  0.610893  0.861127  0.189441  0.294646  0.557034
    3  0.478562  0.571750  0.116209  0.534039  0.869545  0.855520
    4  0.130601  0.678583  0.157052  0.899672  0.093976  0.268974
    
    In [257]: dfs = np.split(df, [4], axis=1)
    
    In [258]: dfs[0]
    Out[258]:
              a         b         c         d
    0  0.823638  0.767999  0.460358  0.034578
    1  0.344320  0.754412  0.274944  0.545039
    2  0.238826  0.610893  0.861127  0.189441
    3  0.478562  0.571750  0.116209  0.534039
    4  0.130601  0.678583  0.157052  0.899672
    
    In [259]: dfs[1]
    Out[259]:
              e         f
    0  0.592420  0.776803
    1  0.031752  0.784564
    2  0.294646  0.557034
    3  0.869545  0.855520
    4  0.093976  0.268974
    

    np.split() is pretty flexible - let's split an original DF into 3 DFs at columns with indexes [2,3]:

    In [260]: dfs = np.split(df, [2,3], axis=1)
    
    In [261]: dfs[0]
    Out[261]:
              a         b
    0  0.823638  0.767999
    1  0.344320  0.754412
    2  0.238826  0.610893
    3  0.478562  0.571750
    4  0.130601  0.678583
    
    In [262]: dfs[1]
    Out[262]:
              c
    0  0.460358
    1  0.274944
    2  0.861127
    3  0.116209
    4  0.157052
    
    In [263]: dfs[2]
    Out[263]:
              d         e         f
    0  0.034578  0.592420  0.776803
    1  0.545039  0.031752  0.784564
    2  0.189441  0.294646  0.557034
    3  0.534039  0.869545  0.855520
    4  0.899672  0.093976  0.268974
    
    0 讨论(0)
提交回复
热议问题