I am new to pandas and am trying to figure out how to transform a dataframe by grouping by rows.. The column names need to be changed depending on data. Any help will be greatly appreciated. I added the image to show original vs transformed view.
Here is the from to expected data
I tried the following code. The data in 2021_b should show 48.3, but with my code it is still showing 32.2. And also, column a should not be repeated, so ultimately, in this example, I should see only 3 rows.
df = pd.DataFrame({
'a':[111, 111, 222, 222, 333, 333],
'b':[2020, 2021, 2020, 2021, 2020, 2021],
'c':[33.2, 48.3, 32.2, 45.5, 45.5, 78.4]
})
df = df.assign(**{'b_'+ str(df['b'].iloc[i]): lambda x: x['c'] for i in range(len(df))})
df.drop('b', axis=1, inplace=True)
df.drop('c', axis=1, inplace=True)
print(df)`