Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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_Pandas - Fatal编程技术网

Python 转换数据帧

Python 转换数据帧,python,pandas,Python,Pandas,我有点麻烦,也许有人能给我指明正确的方向 假设我有一个如下所示的数据框(实际数据集有更多的条目和标识): 有没有快速的方法来重新格式化数据帧,使其看起来像这样 df1 df2 2011-01-01 00:00:00 -1.252090 1.107162 2011-01-01 01:00:00 -1.427444 0.073243 2011-01-01 02:00:00 -0.415251 0.224991 2011

我有点麻烦,也许有人能给我指明正确的方向

假设我有一个如下所示的数据框(实际数据集有更多的条目和标识):

有没有快速的方法来重新格式化数据帧,使其看起来像这样

                         df1        df2
2011-01-01 00:00:00 -1.252090   1.107162   
2011-01-01 01:00:00 -1.427444   0.073243
2011-01-01 02:00:00 -0.415251   0.224991
2011-01-01 03:00:00 -0.797411  -1.269277
2011-01-01 04:00:00 -0.515046   0.468960

我和groupby和transpose玩过,都没有用,任何提示都将不胜感激

您可以使用
pivot
功能:

df.pivot(index='date', columns='variable', values='value')

有关更多信息,请参阅:

我可以通过使用df.map(lamda x:x.startswith(“”)创建索引,然后使用该系列重新创建数据帧来重新创建第二个表。我想我可以循环并附加到数据帧,但是我觉得应该有一种更智能的方法来完成此操作。谢谢,这正是我所寻找的!
df.pivot(index='date', columns='variable', values='value')