0

I have a pandas dataframe like shown below:

enter image description here

From this dataframe, I need output in this format:

enter image description here

Any help would be appreciated

2 Answers2

1

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')
Lara19
  • 615
  • 1
  • 9
  • 20
1

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)
911
  • 542
  • 2
  • 12