本文介绍了 pandas DataFrame透视问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的雷达数据有些奇怪,我无法弄清楚如何使用pandas库正确地旋转数据.

I've got some radar data that's in a bit of an odd format, and I can't figure out how to correctly pivot it using the pandas library.

我的数据:

    speed   time
loc     
A    63  0000
B    61  0000
C    63  0000
D    65  0000
A    73  0005
B    71  0005
C    73  0005
D    75  0005

我想将其转换为如下所示的DataFrame:

I'd like to turn that into a DataFrame that looks like this:

    0000    0005
loc     
A    63     73
B    61     71
C    63     73
D    65     75

我已经做了很多摆弄,但是似乎无法正确理解语法.谁能帮忙吗?

I've done a lot of fiddling around but can't seem to get the syntax correct. Can anyone please help?

谢谢!

推荐答案

您可以在此处使用数据透视方法:

You can use the pivot method here:

In [71]: df
Out[71]: 
     speed  time
loc             
A       63     0
B       61     0
C       63     0
D       65     0
A       73     5
B       71     5
C       73     5
D       75     5

In [72]: df.reset_index().pivot('loc', 'time', 'speed')
Out[72]: 
time   0   5
loc         
A     63  73
B     61  71
C     63  73
D     65  75

这篇关于 pandas DataFrame透视问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 13:34