@@ -33,21 +33,28 @@ 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
- .. ipython :: python
36
+ .. code-block :: python
37
37
38
- from rpy2.robjects import r, pandas2ri
39
- pandas2ri.activate()
38
+ >> > from rpy2.robjects import pandas2ri # doctest: +SKIP
39
+ >> > pandas2ri.activate() # doctest: +SKIP
40
40
41
41
Transferring R data sets into Python
42
42
------------------------------------
43
43
44
44
Once the pandas conversion is activated (``pandas2ri.activate() ``), many conversions
45
45
of R to pandas objects will be done automatically. For example, to obtain the 'iris' dataset as a pandas DataFrame:
46
46
47
- .. ipython :: python
47
+ .. code-block :: python
48
48
49
- r.data(' iris' )
50
- r[' iris' ].head()
49
+ >> > from rpy2.robjects import r # doctest: +SKIP
50
+ >> > r.data(' iris' ) # doctest: +SKIP
51
+ >> > r[' iris' ].head() # doctest: +SKIP
52
+ Sepal.Length Sepal.Width Petal.Length Petal.Width Species
53
+ 0 5.1 3.5 1.4 0.2 setosa
54
+ 1 4.9 3.0 1.4 0.2 setosa
55
+ 2 4.7 3.2 1.3 0.2 setosa
56
+ 3 4.6 3.1 1.5 0.2 setosa
57
+ 4 5.0 3.6 1.4 0.2 setosa
51
58
52
59
If the pandas conversion was not activated, the above could also be accomplished
53
60
by explicitly converting it with the ``pandas2ri.ri2py `` function
@@ -59,13 +66,19 @@ Converting DataFrames into R objects
59
66
The ``pandas2ri.py2ri `` function support the reverse operation to convert
60
67
DataFrames into the equivalent R object (that is, **data.frame **):
61
68
62
- .. ipython :: python
69
+ .. code-block :: python
70
+
71
+ >> > df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ], ' C' : [7 , 8 , 9 ]},
72
+ ... index = [" one" , " two" , " three" ]) # doctest: +SKIP
73
+ >> > r_dataframe = pandas2ri.py2ri(df) # doctest: +SKIP
74
+ >> > print (type (r_dataframe)) # doctest: +SKIP
75
+ < class ' rpy2.robjects.vectors.DataFrame' >
76
+ >> > print (r_dataframe) # doctest: +SKIP
77
+ A B C
78
+ one 1 4 7
79
+ two 2 5 8
80
+ three 3 6 9
63
81
64
- df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ], ' C' :[7 ,8 ,9 ]},
65
- index = [" one" , " two" , " three" ])
66
- r_dataframe = pandas2ri.py2ri(df)
67
- print (type (r_dataframe))
68
- print (r_dataframe)
69
82
70
83
The DataFrame's index is stored as the ``rownames `` attribute of the
71
84
data.frame instance.
0 commit comments