Home » How to Split a Pandas DataFrame into Multiple DataFrames

How to Split a Pandas DataFrame into Multiple DataFrames

by Tutor Aspire

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 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]})

#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.

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

How to Append Two Pandas DataFrames
How to Drop Columns in Pandas DataFrame
How to Select Unique Rows in a Pandas DataFrame

You may also like