Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a pandas DataFrame into multiple dataframes? [duplicate]

I have a dataframe which consists of 231840 rows. I need to split it into 161 separate tables, each table containing 1440 rows, i.e. the first table contains the first 1440 rows, the second table contains the next 1440 rows and so on until I get 161 separate tables with the combined number of rows being 231840 rows. Any ideas?

like image 693
Rauan Saturin Avatar asked Dec 11 '25 17:12

Rauan Saturin


1 Answers

You can use, np.array_split to split the dataframe:

import numpy as np

dfs = np.array_split(df, 161) # split the dataframe into 161 separate tables

Edit (To assign a new col based on sequential number of df in dfs):

dfs = [df.assign(new_col=i) for i, df in enumerate(dfs, 1)]
like image 198
Shubham Sharma Avatar answered Dec 13 '25 07:12

Shubham Sharma