有没有一种简单的方法可以对 Pandas DataFrame 中的列进行分组? [英] Is there an easy way to group columns in a Pandas DataFrame?

查看:57
本文介绍了有没有一种简单的方法可以对 Pandas DataFrame 中的列进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pandas 来表示运动捕捉数据,该数据具有 N 个标记中每个标记的 (x, y, z) 位置的 T 个测量值.例如,当 T=3 和 N=4 时,原始 CSV 数据如下所示:

I am trying to use Pandas to represent motion-capture data, which has T measurements of the (x, y, z) locations of each of N markers. For example, with T=3 and N=4, the raw CSV data looks like:

T,Ax,Ay,Az,Bx,By,Bz,Cx,Cy,Cz,Dx,Dy,Dz
0,1,2,1,3,2,1,4,2,1,5,2,1
1,8,2,3,3,2,9,9,1,3,4,9,1
2,4,5,7,7,7,1,8,3,6,9,2,3

这真的很容易加载到 DataFrame 中,我还学到了一些简单的技巧(例如,将标记数据转换为 z 分数或计算速度).

This is really simple to load into a DataFrame, and I've learned a few tricks that are easy (converting marker data to z-scores, or computing velocities, for example).

不过,我想做的一件事是将上面显示的平面"数据转换为在列(标记)上具有分层索引的格式,以便在级别 0(一个对于每个标记),并且每个标记在级别 1 上都有 3 列(x、y 和 z 各有一个).

One thing I'd like to do, though, is convert the "flat" data shown above into a format that has a hierarchical index on the column (marker), so that there would be N columns at level 0 (one for each marker), and each one of those would have 3 columns at level 1 (one each for x, y, and z).

  A     B     C     D
  x y z x y z x y z x y z
0 1 2 1 3 2 1 4 2 1 5 2 1
1 8 2 3 3 2 9 9 1 3 4 9 1
2 4 5 7 7 7 1 8 3 6 9 2 3

我知道如何通过加载平面文件然后直接操作 Series 对象来完成此操作,可能使用 append 或仅使用手动创建的 MultiIndex 创建一个新的 DataFrame.

I know how do this by loading up the flat file and then manipulating the Series objects directly, perhaps by using append or just creating a new DataFrame using a manually-created MultiIndex.

作为 Pandas 学习者,感觉必须有一种更省力的方法,但很难发现.有没有更简单的方法?

As a Pandas learner, it feels like there must be a way to do this with less effort, but it's hard to discover. Is there an easier way?

推荐答案

在您的情况下,您基本上只需要操作列名.

You basically just need to manipulate the column names, in your case.

从您的原始 DataFrame 开始(以及一个微小的索引操作):

Starting with your original DataFrame (and a tiny index manipulation):

from StringIO import StringIO
import numpy as np
a = pd.read_csv(StringIO('T,Ax,Ay,Az,Bx,By,Bz,Cx,Cy,Cz,Dx,Dy,Dz\n\
    0,1,2,1,3,2,1,4,2,1,5,2,1\n\
    1,8,2,3,3,2,9,9,1,3,4,9,1\n\
    2,4,5,7,7,7,1,8,3,6,9,2,3'))
a.set_index('T', inplace=True)

所以:

>> a
Ax  Ay  Az  Bx  By  Bz  Cx  Cy  Cz  Dx  Dy  Dz
T                                               
0   1   2   1   3   2   1   4   2   1   5   2   1
1   8   2   3   3   2   9   9   1   3   4   9   1
2   4   5   7   7   7   1   8   3   6   9   2   3

然后简单地为您的列创建一个元组列表,并使用 MultiIndex.from_tuples:

Then simply create a list of tuples for your columns, and use MultiIndex.from_tuples:

a.columns = pd.MultiIndex.from_tuples([(c[0], c[1]) for c in a.columns])

>> a
    A           B           C           D
    x   y   z   x   y   z   x   y   z   x   y   z
T                                               
0   1   2   1   3   2   1   4   2   1   5   2   1
1   8   2   3   3   2   9   9   1   3   4   9   1
2   4   5   7   7   7   1   8   3   6   9   2   3

这篇关于有没有一种简单的方法可以对 Pandas DataFrame 中的列进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆