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.

4
  • have you tried making a new data frame and parsing the old one into it with the desired structure? Commented May 24, 2021 at 10:42
  • 2
    I think you can use melt. pandas.pydata.org/pandas-docs/stable/reference/api/… Commented May 24, 2021 at 10:42
  • I am not that good in python, I read the doc. But I wasn't very sure how to proceed. Commented May 24, 2021 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. Commented May 24, 2021 at 10:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.