我目前的问题是对以下链接的跟进问题。
Not able to import pandas in R
我用系统命令在R中执行了python代码。现在在python脚本结束时,我想访问在R中创建的Dataframe。一种方法是使用df.to_csv保存在python中创建的Dataframe,然后在R中导入它。但是我想知道任何直接访问输出的有效方法在R。
@shared_task
def my_task():
time.sleep(5)
Channel('my-background-task').send({"refresh": True})
输出数据帧是:
x=system("/Users/ravinderbhatia/anaconda/bin/python /Users/ravinderbhatia/Downloads/Untitled3.py EMEA regulatory '10% productivity saves SOW'")
X只包含0/1(状态)。如上所述,如何直接在R中访问Dataframe而不保存它。
description status region
10 10% productivity saves SOW pending EMEA
16 10% productivity saves SOW approved EMEA
答案 0 :(得分:0)
我建议您查看reticulate
包(请参阅online vignette)。
您可以使用py_run_file()
运行文件,并使用py
访问python主模块。因此,假设您的文件名为“Untitled3.py”,其创建的数据框称为df
,然后
library(reticulate)
use_python("/Users/ravinderbhatia/anaconda/bin/python")
py_run_file("Untitled3.py")
py$df
修改强>
或者,你只能从python文件中导入函数,只需从里面调用它们。例如,将python文件作为
import pandas as pd
import numpy as np
import sys
from difflib import SequenceMatcher
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def get_similar_CRs(arg1, arg2,arg3):
##create dummy data
cr_id=range(1,41)
description=['change in design','More robust system required',
'Grant system adminstrator rights',
'grant access to all products',
'Increase the credit limit',
'EDAP Scenario',
'Volume prpductivity for NA 2015',
'5% productivity saves SOW',
'effort reduction',
'reduction of false claims',
'Volume productivity EMEA',
'Volume productivity for NA 2016',
'10% productivity saves SOW',
]
region=['EMEA','Asia Pacific','UK']
business=['card','secured loan','mortgage']
type=['regulatory','system','audit']
status=['pending','approved']
data=pd.DataFrame()
data['description']=np.random.choice(description, 40)
data['cr_id']=cr_id
data['region']=np.random.choice(region,40)
data['business']=np.random.choice(business, 40)
data['status']=np.random.choice(status,40)
data['type']=np.random.choice(type,40)
subset_data=data.loc[data.region == arg1]
print (subset_data.head())
subset_data=subset_data.loc[subset_data.type ==arg2]
##This has to be captured dynamically
new_cr=arg3
cr_list=data['description'].unique().tolist()
similar_CR=[] ###global variable
# for new_cr in new_cr_lis
for cr in cr_list:
result=similar(new_cr,cr)
if result >=0.8:
similar_CR.append(cr)
temp=subset_data.loc[subset_data.description.isin(similar_CR)]
temp=temp[['description','status','region']]
return temp
然后运行
library(reticulate)
# To install pandas and numpy in the regular python environment
py_install("pandas", "numpy")
py_run_file("Untitled3.py")
py$get_similar_CRs("EMEA", "regulatory", "10% productivity saves SOW")
#> description status region
#> 2 10% productivity saves SOW pending EMEA
#> 25 10% productivity saves SOW pending EMEA