I try to create a summary table with different measures over time. Therefore, I try to combine several float arrays
such as measure1
into a specific column of the table with the values assigned in the correct rows (years).
This would be a simple example of what I am trying to do:
measure1:
Date
2018 3.075
2019 2.550
2020 3.325
2021 2.475
dtype: float64
Summary:
Measure 1 Measure 2
2018 NaN NaN
2019 NaN NaN
2020 NaN NaN
2021 NaN NaN
Summary:
Measure 1 Measure 2
2018 3.075 NaN
2019 2.550 NaN
2020 3.325 NaN
2021 2.475 NaN
For reproducibility:
Years = ['2018', '2019', '2020', '2021']
Measures = ['Measure 1', 'Measure 2']
Summary = pd.DataFrame(np.nan, index=Years, columns=Measures)
Summary
Thanks a lot!
CodePudding user response:
It is hard to say what exactly you want to do, but in any case you should be able to solve your problem with Dataframe.merge
. Note that I first pack your float values and dates into a separate dataframe:
import pandas as pd
d = {'Measure 1': [3.075, 2.550, 3.325, 2.475], 'Date': ['2018', '2019', '2020', '2021']}
df = pd.DataFrame(data=d).set_index('Date')
years = ['2018', '2019', '2020', '2021']
measures = ['Measure 1', 'Measure 2']
summary = pd.DataFrame(np.nan, index=years, columns=measures).rename_axis('Date')
print(pd.merge(summary, df, on=['Date', 'Measure 1'], how='right'))
Measure 1 Measure 2
Date
2018 3.075 NaN
2019 2.550 NaN
2020 3.325 NaN
2021 2.475 NaN