Split pandas dataframe into multiple dataframes with equal numbers of rows

后端 未结 2 1355
予麋鹿
予麋鹿 2021-01-13 07:32

I have a dataframe df :

        a              b          c
0   0.897134    -0.356157   -0.396212
1   -2.357861   2.066570    -0.512687
2   -0.0         


        
相关标签:
2条回答
  • 2021-01-13 08:01

    There are many ways to do what you want, your method looks over-complicated. A groupby using a scaled index as the grouping key would work:

    df = pd.DataFrame(data=np.random.rand(100, 3), columns=list('ABC'))
    groups = df.groupby(np.arange(len(df.index))/10)
    for (frameno, frame) in groups:
        frame.to_csv("%s.csv" % frameno)
    
    0 讨论(0)
  • 2021-01-13 08:01

    You can use a dictionary comprehension to save slices of the dataframe in groups of ten rows:

    df_dict = {n: df.iloc[n:n+10, :] 
               for n in range(0, len(df), 10)}
    
    >>> df_dict.keys()
    [0, 10, 20]
    
    >>> df_dict[10]
               a         b         c
    10 -0.011909 -0.304162  0.422001
    11  0.127570  0.956831  1.837523
    12 -1.074771  0.379723 -1.889117
    13 -1.449475 -0.799574 -0.878192
    14 -1.029757  0.551023  2.519929
    15 -1.001400  0.838614 -1.006977
    16  0.677216 -0.403859  0.451338
    17  0.221596 -0.323259  0.324158
    18 -0.241935 -2.251687 -0.088494
    19 -0.995426  0.665569 -2.228848
    
    0 讨论(0)
提交回复
热议问题