[Python] How to preserve Labels when SPSS file (.sav) imported into pandas via rpy?

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking to work on a SPSS files (.sav) using pandas. In the absence of the SPSS program, here's what a typical file looks like when converted to .csv:

    [​IMG]

    On investigation into what the first two rows signify (I don't know SPSS), it seems that the first row contains the Labels, while the second row contains the VarNames.

    [​IMG]

    When I bring the file into pandas thus:

    import pandas.rpy.common as com

    def savtocsv(filename):
    w = com.robj.r('foreign::read.spss("%s", to.data.frame=TRUE)' % filename)
    w = com.convert_robj(w)
    return w


    and then do a head(), the first row (Label) is missing:

    [​IMG]

    How can labels be maintained?


    Continue reading...

Compartilhe esta Página