I have an input dataframe like below:
df = pd.DataFrame({'factory': ['kerala', 'kerala', 'kerala', 'delhi', 'delhi', 'goa', 'goa'],
'plant': ['', '', '', '', '', '', ''],
'market': ['', '', '', '', '', '', ''],
'product': ['A', 'B', 'C', 'A', 'B', 'A', 'B'],
'BP4-2023': [4, 4, 5, 6, 4, 5, 5],
'RE4-2023': [7, 7, 8, 8, 7, 8, 8],
'BP5-2023': [4, 4, 5, 6, 4, 5, 5],
'RE5-2023': [7, 7, 8, 8, 7, 8, 8]})
And I want to get the output dataframe like below:
factory plant market product BP4-2023 RE4-2023 BP5-2023 RE5-2023
0 TOTAL
1 A 15 23 15 23
2 B 13 22 13 22
3 C 5 8 5 8
Input dataframe will be dynamic(BP and RE columns).BP
and RE
columns will vary based on months and year.In this A
,B
,C
are product names.Extra products will come,at that time that products will also be added in the output dataframe.
How can I get the below output.Can anyone suggest a solution?