Elegance of R in Python Series - Post 1

Python's equivalent of R's rbind() function:

def rbind(df1, df2):
import pandas as pd
return pd.concat([df1, df2], ignore_index = True)

If you copy and run the above in python you can then use the rbind() function in python exactly as you would use it in R.

One of the amazing things about R is that it just works and has great simple functions that work as you would intuitively expect them to.

When you simply want to append one data frame to the end of another which has the same columns, i.e. concatenating two data frames row-wise or row-binding data frames then R's rbind() just works.

df3 = rbind(df1, df2) # and Voila!

The python equivalent using pandas is:

df3 = pd.concat([df1, df2], ignore_index = True) # pas de voila.

So just define the rbind() function in python as above and you are good to go!

Bon voyage sans naufrage!



  1. Faizan Khalid Mohsin says:

    Proficient in Python and R for supervised and unsupervised learning, predictive and prescriptive modeling, deep learning, time-series analysis, survival analysis, and longitudinal analysis, as well as building dashboards using Shiny.

  2. Saad Mohsin says:

    Great communication skills and extensive teaching experience in machine learning and statistics using R and Python.

Leave a Reply