My question is about rearranging the df1
dataframe to match df2
. I was thinking in filtering by Num
followed by pd.concat(). My question is whether there is a more clever way to do it. Thanks in advance!
df1 = pd.DataFrame({"Month": ["2022-07-01","2022-08-01","2022-09-01","2022-10-01"]*3,
"Num": [10,10,10,10,20,20,20,20,30,30,30,30],
"MB": range(12)}
)
df1["Month"] = pd.to_datetime(df1['Month'])
df2 = pd.DataFrame({"Month": ["2022-06-01"]*3,
"Num": [10, 20, 30],
"MB1": [0,4,8],
"MB2": [1,5,9],
"MB3": [2,6,10],
"MB4": [3,7,11],
}
)
df2["Month"] = pd.to_datetime(df2['Month'])
Try to filtering by Num
and then made a contatenation over all values.