问题
I am new to python and learning pandas. I want to convert a pandas data frame "datframe" to an R-style data frame (to use rpy2 later). To this end I have the following two lines in my code:
import pandas.rpy.common as com
r_dataframe = com.convert_to_r_dataframe(datframe)
The first command goes through but then I get the following error :
Traceback (most recent call last): File "", line 1, in r_dataframe = com.convert_to_r_dataframe(datframe) AttributeError: 'module' object has no attribute 'convert_to_r_dataframe' "
I am not sure why this is happening and how to troubleshoot it. Earlier in the code I have import pandas as pd Could this be the problem?
I am using python 2.7.3, rpy2-2.3.2 and 2.15.3
回答1:
It works for me:
>>> import rpy2
>>> import pandas as pd
>>> import pandas.rpy.common as com
>>> rpy2.__version__
'2.3.1'
>>> pd.__version__
'0.10.0'
>>> datframe = pd.DataFrame({'a' : [1, 2, 3], 'b' : [3, 4, 5]})
>>> r_df = com.convert_to_r_dataframe(datframe)
>>> r_df
<DataFrame - Python:0x105b12710 / R:0x7fa8131d7918>
[IntVector, IntVector]
a: <class 'rpy2.robjects.vectors.IntVector'>
<IntVector - Python:0x105b12ab8 / R:0x7fa8131d7838>
[ 1, 2, 3]
b: <class 'rpy2.robjects.vectors.IntVector'>
<IntVector - Python:0x105b12950 / R:0x7fa8131d7800>
[ 3, 4, 5]
Different rpy2 version though..
来源:https://stackoverflow.com/questions/15209636/convert-to-r-dataframe-module-object-has-no-attribute