The rbind function in R, short for _row-bind_, can be used to group data frames together by the number of rows they have.

We can use the concat() function for pandas to perform the equivalent function in Python.

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

Copy the code

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

Example 1: Using rbind in Python, the number of columns is equal

Let’s say we have 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

Copy the code

We can use the **concat()** function to quickly bind the two DataFrames together in a row.

#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

Copy the code

Note that we can also use **reset_index()** to reset the index 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
Copy the code

Example 2: Using rbind for unequal columns in Python

We can also use the **concat()** function to bind two DataFrame rows with unequal number of columns together, and any missing values will simply be filled in as 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 78 78 78 6 G 88 27.0 7 H 85 27.0 8 I 87 30.0 9 J 95 35.0Copy the code