I have a data frame that looks like this:
The index of my data frame is the "Dates" column.
Dates 3M INDIA LTD ALKYL AMINES CHEMICALS LTD AAVAS FINANCIERS LTD ABB INDIA LTD ADITYA BIRLA CAPITAL LTD
01-01-2020 1.738819 -0.054496 -0.600676 -0.535873 -1.837524 0.514004 -0.853701 -0.101420 2.192982
02-01-2020 -1.110939 3.668744 1.371749 1.346907 4.367026 2.930212 3.540222 4.080081 1.185880
03-01-2020 -0.862856 0.008598 2.543608 2.104247 0.795136 -0.290943 -0.726246 -1.021898 1.368421
06-01-2020 -2.135963 -1.952790 -2.201474 -2.643822 -4.166667 -2.250709 -1.815881 -2.933202 0.300000
07-01-2020 1.692019 8.431578 -1.116379 0.674114 0.097800 -3.166751 0.677638 -1.873767 0.837922
I want to create a new data frame such that for each Date, and Company Name Combination, I'll have 1 row in the data frame.
The resulting data frame will look like this: Dates CompanyName Value
How Can I achieve this transformation using python pandas operations?
df = df.set_index('Dates').stack().reset_index()
df.columns = ['Dates','Company Name','Value']
df.sort_values(by=['Company Name', 'Dates'])
Or
pd.melt(df,
id_vars=['Dates'],
value_vars=[x for x in df.columns if x!='Dates'],
var_name='Company Name',
value_name='Values')