Inverting stacked data
DataFrames have two similar methods, stack
and melt
, to convert horizontal column names into vertical column values. DataFrames have the ability to invert these two operations directly with the unstack
and pivot
methods respectively. stack
/unstack
are simpler methods that allow control over only the column/row indexes, while melt
/pivot
gives more flexibility to choose which columns are reshaped.
Getting ready
In this recipe, we will stack
/melt
a dataset and promptly invert the operation with unstack
/pivot
back to its original form.
How to do it...
- Read in the
college
dataset with the institution name as the index, and with only the undergraduate race columns:
>>> usecol_func = lambda x: 'UGDS_' in x or x == 'INSTNM' >>> college = pd.read_csv('data/college.csv', index_col='INSTNM', usecols=usecol_func) >>> college.head()
- Use the
stack
method to convert each horizontal column name into a vertical...