Visualize Python dataframes (Rstudio)

Preface

To be clear, I have done something very useless. (I will put it for saving the blunt code) I wanted to make it more convenient to use python in Rstudio, so I made a function, but in the end, the convenience has hardly improved. ..

Click here for how to use python in Rstudio.

Python dataframe

Thanks. I love reticulate.

It is common to use pandas for python data frames, but there is a problem that it is difficult to see the data frames when using python with Rstudio.

For example,

#"Data Scientist Association Skill Definition Committee"
#Data of "Data Science 100 Knock (Structured Data Processing)"
#I will use it.

>>> import pandas as pd
>>> df = pd.read_csv("customer.csv")
>>> print(df)

名称未設定.png

Well, it's not bad. .. It's been omitted on the way, so it's a little scary!

Rstudio View () function

The solution is simple.

・ Use google colab obediently (The dataframe looks relatively nice)

-** Use Rstudio-specific View () function **

The View () function is the quickest.

>>> quit #Exit python and return to R

> # library(reticulate)
> View(py$df)

This will bring up a super easy-to-read dataframe that can be moved as usual. 名称未設定.png

Functionalization

Then this

"Loading the reticulate package (py $ cannot be used unless loaded)-> View in Viewer-> Return to python"

Let's make a series of actions like a function.

py_view = function(py_df_name, i_overwrite = FALSE, repl = TRUE){
  library(reticulate)
  #
  if(class(py_df_name) == "character"){
    if((class(try(py$i, T)) == "try-error") |
       (class(try(py$i, T)) != "try-error" & i_overwrite == TRUE)){
      try(py_run_string(paste("i = globals()['", py_df_name, "']",
                              sep = "")), T)
      if(class(try(class(py$i), T)) == "data.frame"){
        View(py$i, py_df_name)
        py_run_string("del i")
        #repl
        if(repl == T){repl_python()}
      }else{
        message(paste("Data frame'", py_df_name, "'Was not found.",
                      sep = ""))
        try(py_run_string("del i"), T)
      }
    }else{
      message("   'i'Exists. Delete or argument i_overwrite =Please set TRUE.")
    }
  }else{
    message("Argument py_df_Please specify the name as a character string.")
  }
}

I had a hard time. First of all, if you set the argument (dataframe name) to "py $ ~", it will end in an instant, but I made it a form to bring in a dataframe with a name that matches in the character string.

The behavior is that if the argument py_df_name is a character (string), move the dataframe to a variable called i in python and look at it with the View () function.

You've created a number of exception handling by try. Commentary ...

Try using

Easy to use.

> py_view("df")
>>> 

With this alone, the previous Viewer is displayed and you can return to python.

Summary

that's all! !! !! It was more wasteful than I expected.

I thought it would be annoying to have to add "py" every time in View () and to stumble with "py $ ~" if I didn't do "library (reticulate)", but in the end I made this I think it's faster to work.

For the time being, it was good practice for exception handling in R.

Recommended Posts

Visualize Python dataframes (Rstudio)
Visualize Keras model in Python 3.5
Write python in Rstudio (reticulate)
Python
Visualize python package dependencies with graphviz
[Python] Visualize the information acquired by Wireshark
Visualize point P that works with Python