Related to this question, I've been trying to use melt
but without success..
I've got a DataFrame with 1 row, like this:
A B C total date A_size B_size C_size total_size
0 4 2 5 11 2019-01-01 123 456 789 1368
Which I'd like to turn into this (at this point I don't care about date
anymore):
Values Sizes
A 4 123
B 2 456
C 5 789
total 11 1368
I've got something terribly hacky that does the job, but it's not flexible. I'd like to be able to add D
and D_size
without having to modify the downstream code.
Hacky code:
def format_table(todays_metadata: pd.DataFrame):
todays_metadata_reformat = todays_metadata.loc[:, 'A':'total'] # hardcoded 'A'
todays_metadata_reformat.index = ['Values']
sizes = todays_metadata.loc[:, 'A_size':'total_size'] # hardcoded 'A_size'
sizes.index = ['Sizes']
sizes.columns = todays_metadata_reformat.columns
todays_metadata_reformat =
todays_metadata_reformat.append(sizes).transpose()
return todays_metadata_reformat