Yes, rbind()
(row bind dataframes) and cbind()
(column bind dataframes) in R are very simple and intuitive.
You can use the "concat()" function from the pandas library for both of them to achieve the same thing. The rbind(df1,df2)
equivalent in pandas will be the following:
pd.concat([df1, df2], ignore_index = True)
However, I have written rbind() and cbind() functions below using pandas for ease of use.
def rbind(df1, df2):
import pandas as pd
return pd.concat([df1, df2], ignore_index = True)
def cbind(df1, df2):
import pandas as pd
# Note this does not keep the original indexes of the df's and resets them to 0,1,...
return pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis = 1)
If you copy, paste, and run the above functions you can use these functions in python the same as you would use them in R. Also, they have the same assumptions as their R counterparts such as for rbind(df1, df2): df1 and df2 need to have the same column names.
Below is an example of the rbind()
function:
import pandas as pd
dict1 = {'Name': ['Ali', 'Craig', 'Shaz', 'Maheen'], 'Age': [36, 38, 33, 34]}
dict2 = {'Name': ['Fahad', 'Tyler', 'Thai-Son', 'Shazmeen', 'Uruj', 'Tatyana'], 'Age': [42, 27, 29, 60, 42, 31]}
data1 = pd.DataFrame(dict1)
data2 = pd.DataFrame(dict2)
# We now row-bind the two dataframes and save it as df_final.
df_final = rbind(data1, data2)
print(df_final)
Here is an open public GitHub repo file I created for writing and consolidating python equivalent R functions in one central place: https://github.com/CubeStatistica/Learning-Data-Science-Properly-for-Work-and-Production-Using-Python/blob/main/Writing-R-Functions-in-Python.ipynb
Feel free to contribute.
Happy coding!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4