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.
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.
I got this
reader = pd.io.stata.StataReader('file.dta')
header = reader.variable_labels()
for var in header:
name = var
label = header[name]
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
.
This will return a dictionary of labels:
>>> pd.io.stata.StataReader('file.dta').variable_labels()
{'x': 'x label', 'y': 'y label'}
reader
is not defined in that answer so it wasn't clear where it came from. From your answer it seems it is from pd.io so that means something new for me. :) –
Bicipital I got this
reader = pd.io.stata.StataReader('file.dta')
header = reader.variable_labels()
for var in header:
name = var
label = header[name]
for
loop though (?) as "header" is already a dictionary. Btw in retrospect I would have just done my answer as a comment but it got two quick upvotes so I decided to leave it. –
Sepulcher © 2022 - 2024 — McMap. All rights reserved.
for
loop though (?) as "header" is already a dictionary. Btw in retrospect I would have just done my answer as a comment but it got two quick upvotes so I decided to leave it. – Sepulcher