I have a data frame that looks like this:
ethnicity_name | enrollment | teacher_salary | ethnicity_percent |
---|---|---|---|
African_American | 513 | 102000 | .10 |
Caucasian | 513 | 102000 | .26 |
Hispanic | 513 | 102000 | .64 |
I need to "unstack" the first column (ethnicity_name) into three additional columns, and fill the values of those columns with "ethnicity_percent" values.
The final dataframe should look like this:
enrollment | teacher_salary | African_American | Caucasian | Hispanic |
---|---|---|---|---|
513 | 102000 | .10 | .26 | .64 |
I can think of the following two ways of doing this, which are hackneyed:
- pd.get_dummies and then df.map on the original names column to the values column
- turn the 'ethnicity_name' column into an index, and unstack, and then map
I'm a beginner, so I have faith that there must be some better way. TIA.