I have a pandas dataframe like shown below:
From this dataframe, I need output in this format:
Any help would be appreciated
I have a pandas dataframe like shown below:
From this dataframe, I need output in this format:
Any help would be appreciated
You can use the pivot functionality to arrange the data in a nice table
df.groupby(['Sector','Date'],as_index = False).sum().pivot('Sector','Date')
Try this way, it should help you.
import pandas as pd
data = {
"Date": [
"2021-03-25",
"2021-03-25",
"2021-03-25",
"2018-08-20",
"2018-08-20",
"2018-08-20",
],
"Sector": ["IT", "FMCG", "Automobile", "IT", "FMCG", "Automobile"],
"Contribution": [40, 30, 30, 60, 25, 15],
}
df = pd.DataFrame(data)
df_pivot = (
df.pivot(index="Sector", columns="Date", values="Contribution")
.reset_index()
.rename_axis(None, axis=1)
)
print(df_pivot)