Home ยป How to Use rbind in Python (Equivalent to R)

How to Use rbind in Python (Equivalent to R)

by Tutor Aspire

The rbind function in R, short for row-bind, can be used to combine data frames together by their rows.

We can use the concat() function from pandas to perform the equivalent function in Python:

df3 = pd.concat([df1, df2])

The following examples shows how to use this function in practice.

Example 1: Use rbind in Python with Equal Columns

Suppose we have the following two pandas DataFrames:

import pandas as pd

#define DataFrames
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E'],
                    'points': [99, 91, 104, 88, 108]})

print(df1)

  team  points
0    A      99
1    B      91
2    C     104
3    D      88
4    E     108

df2 = pd.DataFrame({'assists': ['F', 'G', 'H', 'I', 'J'],
                    'rebounds': [91, 88, 85, 87, 95]})

print(df2)

  team  points
0    F      91
1    G      88
2    H      85
3    I      87
4    J      95

We can use the concat() function to quickly bind these two DataFrames together by their rows:

#row-bind two DataFrames
df3 = pd.concat([df1, df2])

#view resulting DataFrame
df3

	team	points
0	A	99
1	B	91
2	C	104
3	D	88
4	E	108
0	F	91
1	G	88
2	H	85
3	I	87
4	J	95

Note that we can also use reset_index() to reset the index values of the new DataFrame:

#row-bind two DataFrames and reset index values
df3 = pd.concat([df1, df2]).reset_index(drop=True)

#view resulting DataFrame
df3

	team	points
0	A	99
1	B	91
2	C	104
3	D	88
4	E	108
5	F	91
6	G	88
7	H	85
8	I	87
9	J	95

Example 2: Use rbind in Python with Unequal Columns

We can also use the concat() function to row-bind two DataFrames together that have an unequal number of columns and any missing values will simply be filled with NaN:

import pandas as pd

#define DataFrames
df1 = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E'],
                    'points': [99, 91, 104, 88, 108]})

df2 = pd.DataFrame({'team': ['F', 'G', 'H', 'I', 'J'],
                    'points': [91, 88, 85, 87, 95],
                    'rebounds': [24, 27, 27, 30, 35]})

#row-bind two DataFrames
df3 = pd.concat([df1, df2]).reset_index(drop=True)

#view resulting DataFrame
df3

	team	points	rebounds
0	A	99	NaN
1	B	91	NaN
2	C	104	NaN
3	D	88	NaN
4	E	108	NaN
5	F	91	24.0
6	G	88	27.0
7	H	85	27.0
8	I	87	30.0
9	J	95	35.0

Additional Resources

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

How to Use cbind in Python (Equivalent to R)
How to Perform a VLOOKUP in Pandas
How to Drop Rows that Contain a Specific Value in Pandas

You may also like