Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: split dataframe into multiple csvs

I have a large file, imported into a single dataframe in Pandas. I'm using pandas to split up a file into many segments, by the number of rows in the dataframe.

eg: 10 rows: file 1 gets [0:4] file 2 gets [5:9]

Is there a way to do this without having to create more dataframes?

like image 458
billyc59 Avatar asked Aug 16 '26 17:08

billyc59


1 Answers

assign a new column g here, you just need to specific how many item you want in each groupby, here I am using 3 .

df.assign(g=df.index//3)
Out[324]: 
    0  g
0   1  0
1   2  0
2   3  0
3   4  1
4   5  1
5   6  1
6   7  2
7   8  2
8   9  2
9  10  3

and you can call the df[df.g==1] to get what you need

like image 70
BENY Avatar answered Aug 19 '26 23:08

BENY