Skip to main content
 首页 » 编程设计

python之r 网状 : rename duplicates from converted Python pandas dataframe

2024年12月31日10grandyang

我正在使用很棒的新 r 包“reticulate”来合并 Python 和 R,以便能够在 R 中使用数据提供商 (Thomson Reuters Eikon) 的 API,该 API 仅适用于 Python。我希望这样做,因为我的 R 能力优于我的(几乎不存在的)Python 能力。

我使用 Python 模块“eikon”中的函数“get_news_headlines”作为 API 从 Thomson Reuters Eikon 下载数据。我通过将网状函数“import”的参数“convert”设置为 TRUE,自动将生成的 pandas 数据帧转换为 r 数据帧。

API 将下载数据中包含新闻发布日期的第一列设置为索引。当数据框自动转换为 r 对象时,日期中存在重复项,我收到以下错误消息:

Error in `row.names<-.data.frame`(`*tmp*`, value = value) :  
duplicate 'row.names' are not allowed 
In addition: Warning message: 
non-unique value when setting 'row.names': ‘2018-05-31 08:21:56’  

这是我的代码:

library(reticulate) #load reticulate package to combine Python with R 
 
PYTHON_pandas <- import("pandas", convert = TRUE) 
#import Python pandas via reticulate's function "import" 
PYTHON_eikon <- import("eikon", convert = TRUE) 
#import the Thomson Reuters API Python module for use of the API in R 
#(I set convert to true to convert Python objects into their R equivalents) 
 
#do not bother with the following line: 
PYTHON_eikon$set_app_id('ADD EIKON APP ID HERE') 
#set a Thomson Reuters application ID (step is necessary to download data from TR, any string works) 
 
DF <- PYTHON_eikon$get_news_headlines(query = 'Topic:REAM AND Topic:US', count = 10L) 
#save news data from the API in an R dataframe 
#query is the Thomson Reuters code from their Eikon database 
#count is the number of news articles to be downloaded, I arbitrarily set it to 10 articles here 

所以我的问题是,我必须告诉 R 在转换为 r 数据帧之前替换 pandas 索引中的重复项,以避免出现上述错误消息。当我将参数计数设置为一个较小的数字并且碰巧没有任何重复项时,代码可以像现在这样完美地工作。

这对于同时具有 R 和 Python 知识的人来说可能很容易(所以对我来说不是,因为我的 Python 知识非常有限)。不幸的是,代码不可复制,因为我想使用 Thomson Reuters 数据访问。 非常感谢任何帮助!

编辑: 是否可以选择在 import 函数中设置参数 convert = FALSE 以首先在 R 中接收 pandas 数据帧?比我需要有可能在 R 中操作 Python pandas 数据框,以便删除重复项,或者在我手动将 pandas 数据框转换为 R 数据框之前删除 pandas 数据框索引。 reticulate 有可能吗?

eikon Python 包的文档还不是很好,因为它是一个非常新的 Python 模块。

@Moody_Mudskipper:

str(PYTHON_eikon) 仅返回 Module(eikon),因为我仅使用导入函数获取相应的 Python 模块。

names(PYTHON_eikon) 返回: “data_grid” “eikonError” “EikonError” “get_app_id” “get_data” “get_news_headlines” “get_news_story” “get_port_number” “get_symbology” “get_timeout” “get_timeseries” “json_requests” “news_request” “Profile” “send_json_request” “set_app_id” ""set_port_number""set_timeout""symbology""time_series""tools""TR_Field"

所有可用的 eikon 功能似乎都无法帮助我解决问题。

请您参考如下方法:

如果其他人对这个相当特殊的问题感兴趣,我想简单地分享一下我在此期间找到的解决方案(不完美,但有效):

library(reticulate) #load reticulate package to combine Python with R 
 
PYTHON_pandas <- import("pandas", convert = TRUE) 
#import Python pandas via reticulate's function "import" 
PYTHON_eikon <- import("eikon", convert = TRUE) 
#import the Thomson Reuters API Python module for use of the API in R 
#(I set convert to true to convert Python objects into their R equivalents) 
 
#do not bother with the following line: 
PYTHON_eikon$set_app_id('ADD EIKON APP ID HERE') 
#set a Thomson Reuters application ID (step is necessary to download data from TR, any string works) 
 
#**Solution starts HERE:** 
 
DF <- PYTHON_eikon$get_news_headlines(query = 'Topic:REAM AND Topic:US', count = 10L, raw_output = TRUE) 
#use argument "raw_output" to receive a list instead of a dataframe 
 
DF[c(2, 3)] <- NULL 
#delete unrequired list-elements 
 
DF <- list.cbind(DF) 
#use "rlist" function "list.cbind" to column-bind list object "DF" 
 
DF <- rbindlist(DF, fill = FALSE) 
#use "data.table" function "rbindlist" to row-bind list object "DF"