Question

I have a Python list of dictionaries as follows (coming from a sqlite3 row factory):

obs = [{'ave': 0.027, 'pap': 0.277}, 
{'ave': 0.29, 'pap': 0.333}, 
{'ave': 0.25, 'pap': 0.5}]

I would like to convert this to an R data.frame in order to use it with rpy2 (Version 2.3.6), so that it looks like this

    ave   pap  

1 0.027 0.277 
2 0.29  0.333
3 0.25  0.5

I am able to convert a single "row" to a data.frame, as follows:

robjects.DataFrame(obs[0])

    ave   pap 

1 0.027 0.277 

Using robjects.DataFrame(obs) does not work... ValueError: obj can be either an instance of an iter-able class(such a Python dict, rpy2.rlike.container OrdDict or an instance of rpy2.rinterface.SexpVector of type VECSXP

I've also tried to convert it to an OrdDict using rpy2.rlike.container.OrdDict(obs) but got ValueError: too many values to unpack

I think there are many different techniques to achieve this, and the multitude of data structures in both rpy2 and Python confuses me.

Was it helpful?

Solution 2

I actually found an answer (which might not be the most efficient but does the thing for me):

In Python:

df = robjects.DataFrame(obs[0])
for ob in obs[1:]:
    df = df.rbind(robjects.DataFrame(ob))

If somebody has a better, more elegant, more efficient solution, he/she is welcomed to post it.

OTHER TIPS

[In answer to both the question and the accepted answer]

Creating R data frames and appending them will cause performance issues when obs grows larger. One way to address this is to "transpose" the results in Python.

# "transpose" the data structure in Python
from collections import defaultdict
d = defaultdict(list)
for row in obs:
    for colname in row:
        d[colname].append(row[colname])

# Assuming that all data are floats
# (if not the case a mapping between SQLite3 types and R vector types is needed)
for rpy2.robjects.vectors import FloatVector
for colname in d:
    d[colname] = FloatVector(d[colname])

# data frame
from rpy2.robjects import DataFrame
dataf = DataFrame(d)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top