Last updated: 2022-11-08
Checks: 7 0
Knit directory: Pandas-30-R/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20221023) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 685a5b3. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: dev/
Ignored: renv/
Untracked files:
Untracked: analysis/bonus1-complete-all-combinations.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were made to the R Markdown (analysis/method12-joining-dataframes.Rmd) and HTML (docs/method12-joining-dataframes.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | 685a5b3 | Mena WANG | 2022-11-08 | add method12 |
Python to R translation of 30 essential Pandas methods introduced by Avi Chawla in The Only 30 Methods You Should Master To Become A Pandas Pro published on TowardsDataScience.
# enable python in RMarkdown
library(reticulate)
We can use the pd.merge() method to join two dataframes.
import pandas as pd
df1 = pd.DataFrame({'col1': [1,3,5],
'col2': [2,4,6],
'col3': ['A','A','B']})
df2 = pd.DataFrame({'col3': ['A','B','C'],
'col4': ['x','y','z']})
pd.merge(df1, df2, on = 'col3', how = 'right') # the default behavior is an 'inner' join
col1 col2 col3 col4
0 1.0 2.0 A x
1 3.0 4.0 A x
2 5.0 6.0 B y
3 NaN NaN C z
In another article by Avi, he also discussed the advantage of join() over merge() method in terms of run-time performance. Therefore, it would be a good idea to look at how join() works as well.
# set the columns to be joined on as index first
df1.set_index('col3', inplace = True)
df2.set_index('col3', inplace = True)
df = df1.join(df2, how = 'right') # the default behavior is a 'left' join
print(df)
col1 col2 col4
col3
A 1.0 2.0 x
A 3.0 4.0 x
B 5.0 6.0 y
C NaN NaN z
df1.reset_index(inplace = True)
df2.reset_index(inplace = True)
In tidyverse, R has separate functions for inner_join(), left_join(), etc. The columns to be joined on can be specified within the function call.
library(tidyverse)
df1 <- py$df1
df2 <- py$df2
df <- right_join(df1, df2, by = c('col3' = 'col3'))
#since we are joining on a column that has the same name in both dataframes the by= argument can be dropped
df <- right_join(df1, df2)
df
col3 col1 col2 col4
1 A 1 2 x
2 A 3 4 x
3 B 5 6 y
4 C NA NA z
sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.utf8 LC_CTYPE=English_Australia.utf8
[3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Australia.utf8
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] forcats_0.5.2 stringr_1.4.1 dplyr_1.0.10 purrr_0.3.5
[5] readr_2.1.3 tidyr_1.2.1 tibble_3.1.8 ggplot2_3.3.6
[9] tidyverse_1.3.2 reticulate_1.26
loaded via a namespace (and not attached):
[1] httr_1.4.4 sass_0.4.2 jsonlite_1.8.2
[4] modelr_0.1.9 bslib_0.4.0 assertthat_0.2.1
[7] renv_0.16.0 googlesheets4_1.0.1 cellranger_1.1.0
[10] yaml_2.3.5 pillar_1.8.1 backports_1.4.1
[13] lattice_0.20-45 glue_1.6.2 digest_0.6.29
[16] promises_1.2.0.1 rvest_1.0.3 colorspace_2.0-3
[19] htmltools_0.5.3 httpuv_1.6.6 Matrix_1.4-1
[22] pkgconfig_2.0.3 broom_1.0.1 haven_2.5.1
[25] scales_1.2.1 whisker_0.4 later_1.3.0
[28] tzdb_0.3.0 git2r_0.30.1 googledrive_2.0.0
[31] generics_0.1.3 ellipsis_0.3.2 cachem_1.0.6
[34] withr_2.5.0 cli_3.4.1 crayon_1.5.2
[37] magrittr_2.0.3 readxl_1.4.1 evaluate_0.17
[40] fs_1.5.2 fansi_1.0.3 xml2_1.3.3
[43] tools_4.2.1 hms_1.1.2 gargle_1.2.1
[46] lifecycle_1.0.3 munsell_0.5.0 reprex_2.0.2
[49] compiler_4.2.1 jquerylib_0.1.4 rlang_1.0.6
[52] grid_4.2.1 rstudioapi_0.14 rmarkdown_2.17
[55] gtable_0.3.1 DBI_1.1.3 R6_2.5.1
[58] lubridate_1.8.0 knitr_1.40 fastmap_1.1.0
[61] utf8_1.2.2 workflowr_1.7.0 rprojroot_2.0.3
[64] stringi_1.7.8 Rcpp_1.0.9 vctrs_0.4.2
[67] png_0.1-7 dbplyr_2.2.1 tidyselect_1.2.0
[70] xfun_0.33