Pandas - how to convert r dataframe back to pandas?

前端 未结 4 2145
广开言路
广开言路 2020-12-24 01:43

I converted a pandas df to r using the the below:

import pandas as pd
import pandas.rpy.common as com
import rpy2.robjects as ro
from rpy2.robjects.packages          


        
4条回答
  •  离开以前
    2020-12-24 02:18

    Given your import, it appears it is:

    com.convert_robj(rdf)
    

    For example,

    In [480]: dfrm
    Out[480]:
               A          B  C
    0   0.454459  49.916767  1
    1   0.943284  50.878174  1
    2   0.974856  50.335679  2
    3   0.776600  50.782104  1
    4   0.553895  50.084505  1
    5   0.514018  50.719019  2
    6   0.915413  50.513962  0
    7   0.771571  49.859855  2
    8   0.068619  49.409657  0
    9   0.728141  50.945174  2
    10  0.388115  47.879653  1
    11  0.960172  49.680258  0
    12  0.015216  50.067968  0
    13  0.495024  50.286287  1
    14  0.565954  49.909771  1
    15  0.992279  49.009696  1
    16  0.179934  49.554256  0
    17  0.521243  47.854791  0
    18  0.551241  51.076262  1
    19  0.713271  49.418503  0
    20  0.801716  50.660304  1
    
    In [481]: rdfrm = com.convert_to_r_dataframe(dfrm)
    
    In [482]: rdfrm
    Out[482]:
    
    [FloatVector, FloatVector, IntVector]
      A: 
      
    [0.454459, 0.943284, 0.974856, ..., 0.551241, 0.713271, 0.801716]
      B: 
      
    [49.916767, 50.878174, 50.335679, ..., 51.076262, 49.418503, 50.660304]
      C: 
      
    [       1,        1,        2, ...,        1,        0,        1]
    
    In [483]: com.convert_robj(rdfrm)
    Out[483]:
               A          B  C
    0   0.454459  49.916767  1
    1   0.943284  50.878174  1
    2   0.974856  50.335679  2
    3   0.776600  50.782104  1
    4   0.553895  50.084505  1
    5   0.514018  50.719019  2
    6   0.915413  50.513962  0
    7   0.771571  49.859855  2
    8   0.068619  49.409657  0
    9   0.728141  50.945174  2
    10  0.388115  47.879653  1
    11  0.960172  49.680258  0
    12  0.015216  50.067968  0
    13  0.495024  50.286287  1
    14  0.565954  49.909771  1
    15  0.992279  49.009696  1
    16  0.179934  49.554256  0
    17  0.521243  47.854791  0
    18  0.551241  51.076262  1
    19  0.713271  49.418503  0
    20  0.801716  50.660304  1
    

    With docs:

    In [475]: com.convert_robj?
    Type:       function
    String Form:
    File:       /mnt/epd/7.3-2_pandas0.12/lib/python2.7/site-packages/pandas/rpy/common.py
    Definition: com.convert_robj(obj, use_pandas=True)
    Docstring:
    Convert rpy2 object to a pandas-friendly form
    
    Parameters
    ----------
    obj : rpy2 object
    
    Returns
    -------
    Non-rpy data structure, mix of NumPy and pandas objects
    

提交回复
热议问题