[Python] reading RDa file in python as a pandas data frame

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have an RDa file that I created in R. I want to read this file on python as a pandas dataframe. I have the following code to do the same:

    import rpy2.robjects as robjects
    import numpy as np
    from rpy2.robjects import pandas2ri
    pandas2ri.activate()

    # load your file
    robjects.r['load']('Data.RDa')

    matrix = robjects.r['data']

    matrix


    I get the following results:

    R object with classes: ('data.frame',) mapped to:
    <DataFrame - Python:0x0CF46F58 / R:0x0ED0F200>
    [Float..., Float..., Float..., ..., Float..., Float..., Float...]
    area: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0CF56A80 / R:0x0F281898>
    [NA_real_, NA_real_, NA_real_, ..., NA_real_, NA_real_, NA_real_]
    i: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0CF68E68 / R:0x0F2B9520>
    [NA_real_, NA_real_, NA_real_, ..., NA_real_, NA_real_, NA_real_]
    s: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0CF68940 / R:0x0F380008>
    [NA_real_, NA_real_, NA_real_, ..., NA_real_, NA_real_, NA_real_]
    ...
    upslope_area: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0D03FDA0 / R:0x0FE87C90>
    [NA_real_, NA_real_, NA_real_, ..., 292.256494, NA_real_, NA_real_]
    i: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0D03FC88 / R:0x0FEBF918>
    [331347.500000, 331352.500000, 331357.500000, ..., 332187.500000, 332192.500000, 332197.500000]
    s: <class 'rpy2.robjects.vectors.FloatVector'>
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x0D03FE68 / R:0x0FEF75A0>
    [4554812.500000, 4554812.500000, 4554812.500000, ..., 4553982.500000, 4553982.500000, 4553982.500000]


    How do I convert this to a pandas data frame?

    Continue reading...

Compartilhe esta Página