1

I need to unpivot a dataset with the column names in date format. To un-pivot, I would need to call column names, but they would keep changing every month; hence, I cannot use column names because of their dynamic nature. Here is an example of the table:

enter image description here

I need to un-pivot these date columns and I cannot call column names as these columns may change next month. Here is the desired output:

enter image description here

Could you please help me with any solution for this in spark SQL or Pandas as I am using Palantir foundry.

Note:

  1. There are hundreds of rows in data, this only one row is a sample example.
  2. If I rename column names, changing them back to date columns would be difficult as well.

Thanks.

Shazib Munshi
  • 49
  • 1
  • 5

1 Answers1

3

You can use melt() to achieve this

pd.melt(df, id_vars=df.columns[:3], value_vars=df.columns[3:], 
var_name='Date', value_name='Value')
Sanidhya Singh
  • 441
  • 3
  • 11