How to import pandas dataframe into R dataframe ?

How to import pandas dataframe into R dataframe ?

I do not want to use csv way, meaning saving into csv and read as csv into R.

What is the best way to do it ?

As an example, if you are working in R Markdown, you can use the {reticulate} package.

In an .Rmd:

---
title: "Importing pandas data frame as an R data frame"
---

```{python}
import pandas as pd
dat_py = pd.DataFrame({'A': [1, 2, 3]})
```

```{r}
library(reticulate)
dat_r <- py$dat_py
```

The pandas data frame dat_py is now in the R data frame dat_r.

The reticulate cheat sheet is very helpful.

1 Like

Hi and thank you,

Is in reticulate or in python an equivalent for R's dput() ?

Hi there, not 100% sure if this is what you mean, but once you create the R object, you can use it like any other R object.

---
title: "Importing pandas data frame as an R data frame"
---

```{python}
import pandas as pd
dat_py = pd.DataFrame({'A': [1, 2, 3]})
```

```{r}
library(reticulate)
dat_r <- py$dat_py

dput(dat_r)
```

Equivalent of dput() but in python I meant, not in R.

for a pandas dataframe i think there is to_dict method

link

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.