Georg Heiler Georg Heiler - 2 months ago 5
Python Question

How to get data from R to pandas

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
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
pull from R

edit



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

Answer

Just slice the ListVector:

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

Image

If you want a dictionary instead:

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