简体   繁体   中英

How to import pandas using R studio

So, just to be clear, I'm very new to python coding... so I'm not exactly sure what's going wrong.

Yesterday, whilst following a tutorial on calling python from R, I successfully installed and used several python packages (eg, NumPy, pandas, matplotlib etc).

But today, when trying to run the exact same code, I'm getting an error when trying to import pandas (NumPy is importing without any errors). The error states:

ModuleNotFoundError: No module named 'pandas'

I'm not sure what's going on!? I'm using R-Studio (running on a Mac)... here's a code snippet of how I'm doing it:

library(reticulate) 
os <- import("os") # Setting directory
os$getcwd()
repl_python()       #used to make it interactive 
import numpy as np. # Load numpy  package
import pandas as pd # Load pandas package

At this point, it's throwing me an error. I've tried googling the answer and searching here, but to no avail.

Any suggestions as to how I'd fix this problem, or what is going on? Thanks

Possibly your python path for reticulate changed upon reloading Rstudio. Here is how to set the path manually (filepath for Linux or Mac):

library(reticulate)
path_to_python <- "~/anaconda3/bin/python"
use_python(path_to_python)

https://stackoverflow.com/a/45891929/4549682

You can check your Python path with py_config() : https://rstudio.github.io/reticulate/articles/versions.html#configuration-info

I recommend using Anaconda for your Python distribution (you might have to use Anaconda anyway for reticulate, not sure). Download it from here: https://www.anaconda.com/distribution/#download-section Then you can create the environment for reticulate to use:

conda_create('r-reticulate', packages = "python=3.5")

I use Python 3.5 for some specific packages, but you can change that version or leave it as just 'python' for the latest version. https://www.rdocumentation.org/packages/reticulate/versions/1.10/topics/conda-tools

Then you want to install the packages you need (if they aren't already) with

conda_install('re-reticulate', packages = 'numpy')

The way I use something like numpy is

np <- import('numpy')
np$arange(10)

You need to set the second argument of the function use_python, so it should be:

For example, use_python("/users/my_user/Anaconda3/python.exe",required = TRUE)

DON'T forget required = TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM