1

I have the following df:

              Year           b      c     Monthly Flow (2018)    First thing  Second thing Third thing  
1             2018   -0.041619  43.91               -0.041619           2000          1000           6
2             2018    0.011913  43.91               -0.041619           4000          120            8
3             2018   -0.048801  43.91               -0.041619           2000          1000           6
4             2018    0.002857  43.91               -0.041619           2000          1000           6

And I would like restructure it in order to achieve this output:

              Year           b      c     Monthly Flow (2018)    Process name    Value  
1             2018   -0.041619  43.91               -0.041619     First thing     2000
1             2018    0.011913  43.91               -0.041619    Second thing     1000
1             2018   -0.048801  43.91               -0.041619     Third thing        6
2             2018   -0.041619  43.91               -0.041619     First thing     4000
2             2018    0.011913  43.91               -0.041619    Second thing      120
2             2018   -0.048801  43.91               -0.041619     Third thing        8 
...

I tried to pivot but I can't get this output.

1 Answer 1

4

You can use df.melt:

df.melt(id_vars=['Year','b','c','Monthly Flow (2018)'], var_name='Process name',value_name='Value')

Output:

    Year     b     c  Monthly_Flow_(2018)  Process name  Value
0   2018 -0.04 43.91                -0.04   First_thing   2000
1   2018  0.01 43.91                -0.04   First_thing   4000
2   2018 -0.05 43.91                -0.04   First_thing   2000
3   2018  0.00 43.91                -0.04   First_thing   2000
4   2018 -0.04 43.91                -0.04  Second_thing   1000
5   2018  0.01 43.91                -0.04  Second_thing    120
6   2018 -0.05 43.91                -0.04  Second_thing   1000
7   2018  0.00 43.91                -0.04  Second_thing   1000
8   2018 -0.04 43.91                -0.04   Third_thing      6
9   2018  0.01 43.91                -0.04   Third_thing      8
10  2018 -0.05 43.91                -0.04   Third_thing      6
11  2018  0.00 43.91                -0.04   Third_thing      6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.