Alam
Alam

Reputation: 403

Is there a way to read Stata labels in python?

df = pd.read_stata('file.dta')
for cols in df.columns.values:
    name = cols.lower()
    type = df[cols].dtype
    #label = ...

I need to get the labels/descriptions in python for each column.

Upvotes: 7

Views: 4762

Answers (3)

JohnE
JohnE

Reputation: 30424

This will return a dictionary of labels:

>>> pd.io.stata.StataReader('file.dta').variable_labels()
{'x': 'x label', 'y': 'y label'}

Upvotes: 4

Kyle Barron
Kyle Barron

Reputation: 2649

In Pandas 0.22, you can also access this by creation of the iterator. I.e.

import pandas as pd
itr = pd.read_stata('file.dta', iterator=True)
itr.variable_labels()

This will return a dictionary where the keys are variable names and the values are variable labels. I think this is easier to remember than pd.io.stata.StataReader.

Upvotes: 9

Alam
Alam

Reputation: 403

I got this

reader = pd.io.stata.StataReader('file.dta')
header = reader.variable_labels()
for var in header:
    name = var
    label = header[name]

Upvotes: 1

Related Questions