I have this excel table read in jupyter notebook with pandas. I want to melt the upper row side of the table into column. The table looks like as follow:
ori code cgk cgk clg clg
ori city jakarta NaN cilegon NaN
ori prop jakarta NaN banten NaN
ori area jawa NaN jawa NaN
code city district island type a days type b days
001 jakarta jakarta jawa 12000 2 13000 3
002 surabaya surabaya jawa 13000 3 14000 4
I realized that df.melt should be worked to transpose the upper rows, but the type
& days
columns, and also the 4 rows and the NaN value on it get me confuse on how to do that correctly.
The desire clean dataframe I need is as follow:
code city district island type price_type days ori_code ori_city ori_prop ori_area
001 jakarta jakarta jawa type a 12000 2 cgk jakarta jakarta jawa
001 jakarta jakarta jawa type b 13000 3 clg cilegon banten jawa
002 surabaya surabaya jawa type a 13000 3 cgk jakarta jakarta jawa
002 surabaya surabaya jawa type b 14000 4 clg cilegon banten jawa
The ori_code, ori_city, ori_prop, ori_area
would become column names.
So far what I have done is set fix index name which are code, city, district and also island.
df = df.set_index(['code','city','district','island'])
can anyone help me to solve this problem? Any helps would be much appreciated. Thank you in advance.