Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以特定的方式重构数据帧?_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 如何以特定的方式重构数据帧?

Python 如何以特定的方式重构数据帧?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有以下建议: 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

我有以下建议:

              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

我想对其进行重组,以实现这一产出:

              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 
...
我试图透视,但无法获得此输出。

您可以使用

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

    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
您可以使用

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

    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

@爱德华多2111请不要忘记对答案投赞成票。@爱德华多2111请不要忘记对答案投反对票。