reading rds file in python

676 Views Asked by At

When read rds file in python following way:

import pyreadr
df = prreadr.read_r('/home/test.rds')

to turn df into DataFrame, I tried:

pd.DataFrame(df) - I get If using all scalar values, you must pass an index message

pd.DataFrame(df, columns = ['a','b','c']) - I get columns with NaN

pd.DataFrame.from_dict(df, orient = 'index', columns = ['a','b','c']) - I get Shape of passed values is (1, 1), indices imply (1, 11) message

I want to read rds file in Python and turn it into DataFrame.

1

There are 1 best solutions below

0
Otto Fajardo On

when using read_r, it returns a dictionary. You should first look at what keys you got in your dictionary, each of those elements is a dataframe. In the case of rdata files y can multiple dataframes per file, that is why you can get multiple elements in your dictionary. In the case of rds files you can get only one element and therefore you have only one element with the key None. So do this (observe the None in square brackets)

import pyreadr
df = prreadr.read_r('/home/test.rds')[None]