I have a dataframe like this, but much larger:
velocity mass volume acceleration temperature pressure
0 100 4.5 12 1.3 45 6.5
1 120 5.5 15 2.1 60 7
2 130 6 11 2 55 12
I would like to add the units of measurements (UOMs) to the second row. However, the challenge I am having is that I would like to add the UOMs depending on the value of the first row, instead of hard coding them. For instance, if the first row/first column is velocity, I want the row right below it to have a UOM of 'm/s', and if it is a mass, then the UOM must be 'kg' and so on.
I was thinking perhaps it would be a good idea to create a dictionary, like: {'velocity': 'm/s', 'mass':'kg', etc} and then write a looping logic, where if the row value is the key in a dictionary, then assign the value of it.
The output df I want is as follows:
velocity mass volume acceleration temperature pressure
m/s kg gal m/s2 deg C atm
0 100 4.5 12 1.3 45 6.5
1 120 5.5 15 2.1 60 7
2 130 6 11 2 55 12
CodePudding user response:
I think yes you can define a dict first and go like:
uom_dict={'velocity':'m/s',
'mass': 'kg',
'volume':'gal',
'acceleration': 'm/s2',
'temperature':'deg C',
'pressure':'atm'}
df.columns = pd.MultiIndex.from_arrays([df.columns, df.columns.map(uom_dict)])
output:
velocity mass volume acceleration temperature pressure
m/s kg gal m/s2 deg C atm
0 100 4.5 12 1.3 45 6.5
1 120 5.5 15 2.1 60 7.0
2 130 6.0 11 2.0 55 12.0