@@ -33,22 +33,27 @@ See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project:
33
33
34
34
In the remainder of this page, a few examples of explicit conversion is given. The pandas conversion of rpy2 needs first to be activated:
35
35
36
- .. code-block :: python
36
+ .. ipython :: ipython
37
+ :verbatim:
37
38
38
- >> > from rpy2.robjects import pandas2ri
39
- >> > pandas2ri.activate()
39
+ In [1]: from rpy2.robjects import pandas2ri
40
+ ...: pandas2ri.activate()
40
41
41
42
Transferring R data sets into Python
42
43
------------------------------------
43
44
44
45
Once the pandas conversion is activated (``pandas2ri.activate() ``), many conversions
45
46
of R to pandas objects will be done automatically. For example, to obtain the 'iris' dataset as a pandas DataFrame:
46
47
47
- .. code-block :: python
48
+ .. ipython :: ipython
49
+ :verbatim:
48
50
49
- >> > from rpy2.robjects import r
50
- >> > r.data(' iris' )
51
- >> > r[' iris' ].head()
51
+ In [2]: from rpy2.robjects import r
52
+
53
+ In [3]: r.data('iris')
54
+
55
+ In [4]: r['iris'].head()
56
+ Out[4]:
52
57
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
53
58
0 5.1 3.5 1.4 0.2 setosa
54
59
1 4.9 3.0 1.4 0.2 setosa
@@ -66,19 +71,25 @@ Converting DataFrames into R objects
66
71
The ``pandas2ri.py2ri `` function support the reverse operation to convert
67
72
DataFrames into the equivalent R object (that is, **data.frame **):
68
73
69
- .. code-block :: python
74
+ .. ipython :: ipython
75
+ :verbatim:
76
+
77
+ In [5]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
78
+ ...: index=["one", "two", "three"])
70
79
71
- >> > df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ], ' C' : [7 , 8 , 9 ]},
72
- ... index = [" one" , " two" , " three" ])
73
- >> > r_dataframe = pandas2ri.py2ri(df)
74
- >> > print (type (r_dataframe))
75
- < class ' rpy2.robjects.vectors.DataFrame' >
76
- >> > print (r_dataframe)
80
+ In [6]: r_dataframe = pandas2ri.py2ri(df)
81
+
82
+ In [7]: print(type(r_dataframe))
83
+ Out[7]: <class 'rpy2.robjects.vectors.DataFrame'>
84
+
85
+ In [8]: print(r_dataframe)
86
+ Out[8]:
77
87
A B C
78
88
one 1 4 7
79
89
two 2 5 8
80
90
three 3 6 9
81
91
92
+
82
93
The DataFrame's index is stored as the ``rownames `` attribute of the
83
94
data.frame instance.
84
95
0 commit comments