Table of Contents
A Pandas DataFrame can be split into multiple DataFrames by using the “groupby” function, which allows for splitting the data based on a specific column or set of columns. This function creates a “groupby” object, which can then be iterated over to access each individual group and convert it into a separate DataFrame. Additionally, the “split” function can also be used to divide a DataFrame into multiple smaller DataFrames based on a chosen condition. These methods allow for efficient and organized splitting of data within a Pandas DataFrame.
Split a Pandas DataFrame into Multiple DataFrames
You can use the following basic syntax to split a pandas DataFrame into multiple DataFrames based on row number:
#split DataFrame into two DataFrames at row 6 df1 = df.iloc[:6] df2 = df.iloc[6:]
The following examples show how to use this syntax in practice.
Example 1: Split Pandas DataFrame into Two DataFrames
The following code shows how to split one pandas DataFrame into two DataFrames:
import pandas as pd #create DataFrame df = pd.DataFrame({'x': [1, 1, 1, 3, 3, 4, 5, 5, 5, 6, 7, 9], 'y': [5, 7, 7, 9, 12, 9, 9, 4, 3, 3, 1, 10]}) #view DataFrame df x y 0 1 5 1 1 7 2 1 7 3 3 9 4 3 12 5 4 9 6 5 9 7 5 4 8 5 3 9 6 3 10 7 1 11 9 10 #split original DataFrame into two DataFrames df1 = df.iloc[:6] df2 = df.iloc[6:] #view resulting DataFrames print(df1) x y 0 1 5 1 1 7 2 1 7 3 3 9 4 3 12 5 4 9 print(df2) x y 6 5 9 7 5 4 8 5 3 9 6 3 10 7 1 11 9 10
Notice that df1 contains the first six rows of the original DataFrame and df2 contains the last six rows of the original DataFrame.
Example 2: Split Pandas DataFrame into Multiple DataFrames
The following code shows how to split a pandas
import pandas as pd #create DataFramedf = pd.DataFrame({'x': [1, 1, 1, 3, 3, 4, 5, 5, 5, 6, 7, 9], 'y': [5, 7, 7, 9, 12, 9, 9, 4, 3, 3, 1, 10]}) #split into three DataFrames df1 = df.iloc[:3] df2 = df.iloc[3:6] df3 = df.iloc[6:] #view resulting DataFrames print(df1) x y 0 1 5 1 1 7 2 1 7 print(df2) x y 3 3 9 4 3 12 5 4 9 print(df3) x y 6 5 9 7 5 4 8 5 3 9 6 3 10 7 1 11 9 10
In this example we chose to split one DataFrame into three DataFrames, but using this syntax we can split a pandas DataFrame into any number of DataFrames that we’d like.
The following tutorials explain how to perform other common functions in pandas:
Cite this article
stats writer (2024). How can a Pandas DataFrame be split into multiple DataFrames?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-be-split-into-multiple-dataframes/
stats writer. "How can a Pandas DataFrame be split into multiple DataFrames?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-be-split-into-multiple-dataframes/.
stats writer. "How can a Pandas DataFrame be split into multiple DataFrames?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-be-split-into-multiple-dataframes/.
stats writer (2024) 'How can a Pandas DataFrame be split into multiple DataFrames?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-be-split-into-multiple-dataframes/.
[1] stats writer, "How can a Pandas DataFrame be split into multiple DataFrames?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can a Pandas DataFrame be split into multiple DataFrames?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
