2
check_NUMBER,2021-12-01,2022-01-01,2022-02-01
1000M,24,17,22
1000M,24,83,55

This is my example data. I want to convert it into:

check_NUMBER,dates,values
1000M,2021-12-01,24
1000M,2021-12-01,17
1000M,2021-12-01,22
1000M,2022-02-01,24
1000M,2022-02-01,83
1000M,2022-02-01,55

I am unable to achieve this. Can someone explain how we can do that.

LearnerJS
  • 298
  • 2
  • 14
  • have you tried making a new data frame and parsing the old one into it with the desired structure? – Chris May 24 '21 at 10:42
  • 2
    I think you can use melt. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.melt.html#pandas.DataFrame.melt – Shashwat May 24 '21 at 10:42
  • I am not that good in python, I read the doc. But I wasn't very sure how to proceed. – LearnerJS May 24 '21 at 10:44
  • I'm baffled by what you're trying to achieve, why are you associating 17 and 22 with 2021-12-01 ? And 24, 83 with 2022-02-01 ? I fail to see the logic. Using `df.melt` makes sense when I look at the original dataframe, but it doesn't produce what you want to convert it into. – joao May 24 '21 at 10:57

1 Answers1

2

Use df.melt

Code

df.melt(id_vars='check_NUMBER', var_name='dates', value_name='values')

Output

check_NUMBER    dates   values
0   1000M   2021-12-01  24
1   1000M   2021-12-01  24
2   1000M   2022-01-01  17
3   1000M   2022-01-01  83
4   1000M   2022-02-01  22
5   1000M   2022-02-01  55
Utsav
  • 5,572
  • 2
  • 29
  • 43