How can I use rbind in Python to combine two data frames, similar to the rbind function in R?

How can I use rbind in Python to combine two data frames, similar to the rbind function in R?

The rbind function in R is used to combine two data frames vertically, by appending the rows of one data frame to the end of the other. In order to achieve a similar result in Python, the pandas library offers the “concat” function. This function allows you to combine two data frames row-wise, similar to the rbind function in R. By specifying the “axis” parameter as 0, the rows of one data frame can be appended to the rows of another data frame, creating a new combined data frame. This can be useful for merging data from multiple sources into one cohesive data set.

Use rbind in Python (Equivalent to R)


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

We can use the 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:

Cite this article

stats writer (2024). How can I use rbind in Python to combine two data frames, similar to the rbind function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-rbind-in-python-to-combine-two-data-frames-similar-to-the-rbind-function-in-r/

stats writer. "How can I use rbind in Python to combine two data frames, similar to the rbind function in R?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-rbind-in-python-to-combine-two-data-frames-similar-to-the-rbind-function-in-r/.

stats writer. "How can I use rbind in Python to combine two data frames, similar to the rbind function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-rbind-in-python-to-combine-two-data-frames-similar-to-the-rbind-function-in-r/.

stats writer (2024) 'How can I use rbind in Python to combine two data frames, similar to the rbind function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-rbind-in-python-to-combine-two-data-frames-similar-to-the-rbind-function-in-r/.

[1] stats writer, "How can I use rbind in Python to combine two data frames, similar to the rbind function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I use rbind in Python to combine two data frames, similar to the rbind function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top