I have a dataframe that looks like this
GEO_NAME CHARACTERISTIC_NAME C1_COUNT_TOTAL C2_COUNT_MEN+ C3_COUNT_WOMEN+
60010188 0 to 14 years 105.0 50.0 50.0
60010188 15 to 19 years 30.0 15.0 15.0
60010188 20 to 24 years 20.0 10.0 15.0
60010188 25 to 29 years 35.0 15.0 20.0
60010188 30 to 34 years 35.0 15.0 20.0
Here GEO_NAME
is a geographic area, and CHARACTERISTIC_NAME
are the names of the variable, with the total count, men count and women count per geography. I would like to turn the dataframe from long to wide, so something like this where each geography is just one row and all of the columns are then appended.
GEO_NAME 0 to 14 years_TOTAL 0 to 14 years_MEN 0 to 14 years_WOMEN 15 to 19 years_TOTAL ...
60010188 105 50 50 30 ...
....
How would I do this in pandas?
you can using pivot:
example:
df_pivot = df.pivot_table(index='GEO_NAME', columns='CHARACTERISTIC_NAME', values=['C1_COUNT_TOTAL', 'C2_COUNT_MEN+', 'C3_COUNT_WOMEN+'])
# Flatten the MultiIndex columns
df_pivot.columns = ['_'.join(col).strip() for col in df_pivot.columns.values]
# Reset index if needed
df_pivot.reset_index(inplace=True)
print(df_pivot)