How to import R-frames of data in Pandas?

I use R off and is included as a "backend" for Python, and therefore, it is necessary to import dataframes from R from Python from time to time; but I can't figure out how to import R data.frameas Pandas DataFrame.

For example, if I create a dataframe in R

rdf = data.frame(a=c(2, 3, 5), b=c("aa", "bb", "cc"), c=c(TRUE, FALSE, TRUE))

and then pull it in python using rmagicwith

%Rpull -d rdf

I get

array([(2.0, 1, 1), (3.0, 2, 0), (5.0, 3, 1)], 
      dtype=[('a', '<f8'), ('b', '<i4'), ('c', '<i4')])

I don’t know what it is, and it certainly doesn’t

pd.DataFrame({'a': [2, 3, 5], 'b': ['aa', 'bb', 'cc'], 'c': [True, False, True]})

which I would expect.

The only thing that suits me to work is to use a file to transfer data, writing to R

write.csv(data.frame(a=c(2, 3, 5), b=c("aa", "bb", "cc"), c=c(TRUE, FALSE, TRUE)), file="TEST.csv")

and then reading in Python

pd.read_csv("TEST.csv")

although even this approach creates an additional column: "Unnamed: 0".

What is the idiom for importing an R data frame in Python as a Pandas dataframe?

+4
2

: array([(2.0, 1, 1), (3.0, 2, 0), (5.0, 3, 1)], dtype=[('a', '<f8'), ('b', '<i4'), ('c', '<i4')]). numpy array. http://docs.scipy.org/doc/numpy/user/basics.rec.html/. pandas DF pd.DataFrame:

In [65]:

from numpy import *
print pd.DataFrame(array([(2.0, 1, 1), (3.0, 2, 0), (5.0, 3, 1)], dtype=[('a', '<f8'), ('b', '<i4'), ('c', '<i4')]))
   a  b  c
0  2  1  1
1  3  2  0
2  5  3  1

b ( factor() 'ed R), c boolean int. a int float ('<f8', )

-, , pandas.rpy.common - R: http://pandas.pydata.org/pandas-docs/stable/r_interface.html (, , ):

In [71]:

import pandas.rpy.common as com
DF=pd.DataFrame({'val':[1,1,1,2,2,3,3]})
r_DF = com.convert_to_r_dataframe(DF)
print pd.DataFrame(com.convert_robj(r_DF))
   val
0    1
1    1
2    1
3    2
4    2
5    3
6    3

, Unnamed: 0 . , index_col=0 pd.read_csv()

+6

(. pandas 0.13.1 ):

%load_ext rmagic
%R rdf = data.frame(a=c(2, 3, 5), b=c("aa", "bb", "cc"), c=c(TRUE, FALSE, TRUE))

import pandas.rpy.common as com

print com.load_data('rdf')
   a   b      c
1  2  aa   True
2  3  bb  False
3  5  cc   True
+2

Source: https://habr.com/ru/post/1534065/


All Articles