I need some help to solve this problem. I have a panda dataframe that has 101 unique columns[Column A to Column CW)]. I need to unpivot them into row-based output.
Original Data frame;
HSP CAT AMK COL OPK ROI GIO DOL values
0 0 0 0 0 0 0 0 0.365
0 0 0 0 0 0 0 0 0.8454
0 0 0 0 0 0 0 0 0.74654
0 0 0 0 0 0 0 0 0.74654
Output should be;
Industry LGA values
HSP OPK 0.365
CAT ROI 0.8454
AMK GOI 0.74654
COL DOL 0.74654
I have tested a function called melt() to unpivot 3 columns. I have tested it for the first time. I am not sure how to provide all column names dynamically within the same function. so that I don't have to inject every column name in that function. Here is my sample code;
df1= pd.melt(df, id_vars=['values'], value_vars= ['HSP', 'CAT','AMK', 'COL']) << same for another var called 'LGA'
Unfortunately its not working based on my requirement. I have columns (A to U) which should be captured in "Industry" and Column (v to CV) should be captured in "LGA" column. Not sure how to create two "value_bars" parameters. I know that there will be another column called "value" with 1 in each row. I can drop that column later. I am just trying to find a way provide all columns(A:CW) into two "value_vars" parameter section. Not sure how!
Tried this to impute the column to rows dynamically;
X_testP = ({"Industry": X_test.iloc[A:U].columns,
"LGA": X_test.iloc[V:CV].columns,
"values": X_test.iloc['values']})
Unable to get the desired output.
Any help would be appreciated.
Thanks in advance
>>> pd.DataFrame({"Industry": df.filter(like="Ind").columns,
"LGA": df.filter(like="LGA").columns,
"values": df["values"]})
Industry LGA values
0 Ind 1 LGA1 0.36500
1 Ind 2 LGA2 0.84540
2 Ind 3 LGA3 0.74654
3 Ind 4 LGA4 0.74654
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.