如何从R到 pandas 获取数据 [英] How to get data from R to pandas

查看:97
本文介绍了如何从R到 pandas 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在木星笔记本中,我在R中创建了一些2-d-list

In a jupiter notebook I created some 2-d-list in R like

%%R
first <- "first"
second <- "second"

names(first) <- "first_thing"
names(second) <- "second_thing"

x <- list()
index <- length(x)+1
x[[index]] = first
x[[index +1]] = second

a %Rpull x不会返回漂亮的表示形式,而是一个ListVector.我如何将其转换为更好的东西dict/pd.Dataframe?到目前为止,我没有遇到 http://pandas.pydata.org/pandas -docs/stable/r_interface.html

a %Rpull x does not return the nice representation but rather a ListVector. How can I convert it into something nicer e.g. a dict / pd.Dataframe? So far I had no luck following http://pandas.pydata.org/pandas-docs/stable/r_interface.html

我要转换的列表是一个二维列表,例如results从上面截取的更新代码

The list I want to convert is a 2-d list like results the updated code snipped from above

推荐答案

只需切片ListVector:

%Rpull x
pd.DataFrame(data=[i[0] for i in x], columns=['X'])

如果您想要字典,则:

dict([[i,j[0]] for i,j in enumerate(x)])
{0: 'first', 1: 'first'}

这篇关于如何从R到 pandas 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆