Predictive Hacks

How to split a Pandas Dataframe into equal parts

We can use numpy to split a dataframe into N equal parts as follows:

import pandas as pd
import numpy as np

df=pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9]})
df

Now let’s split the Dataframe into 3 equal parts.

df_split = np.array_split(df, 3)

Now you can treat the df_split as a list of dataframes.

df_split[0]
df_split[1]
df_split[2]

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.