简体   繁体   English

如何在 pivot_table 操作后访问列(multiIndex 数据帧)

[英]How to access the columns after pivot_table operation (multiIndex dataframes)

I initially had this dataframe:我最初有这个 dataframe:

    df = pd.DataFrame({'slide': [0, 0, 1, 1, 2, 2, 0, 0],
                           'time': [1673, 17892, 1132, 61730, 2323, 8491, 3958, 3432],
                           'frame': ['-1', '0', '-1', '0', '-1', '0', '-1', '0'],
                           'id': [1111, 1111, 1132, 1132, 4636, 4636, 7711, 7711],
                           'name': ['foo', 'foo', 'bar', 'bar', 'zoo', 'zoo', 'baz', 'baz']})
        df
           slide   time frame    id name
        0      0   1673    -1  1111  foo
        1      0  17892     0  1111  foo
        2      1   1132    -1  1132  bar
        3      1  61730     0  1132  bar
        4      2   2323    -1  4636  zoo
        5      2   8491     0  4636  zoo
        6      0   3958    -1  7711  baz
        7      0   3432     0  7711  baz

I did pivot_table to get the following:我做了 pivot_table 得到以下内容:


    pd.pivot_table(df,index = ['id','name'], values = 'time',columns = ['frame']).astype(int)

    df
    frame        -1      0
    id   name             
    1111 foo   1673  17892
    1132 bar   1132  61730
    4636 zoo   2323   8491
    7711 baz   3958   3432

I want to make the resulting dataframe as the following way (see below) but I don't know how to go about this.我想按照以下方式生成结果 dataframe(见下文),但我不知道如何生成 go。 I am new to pandas and just found out that multiindexing exist.. I tried accessing the columns with df['0']['id'] and also with df['0','id'] but both would throw me errors..... Any help will be appreciated!我是 pandas 的新手,刚刚发现存在多索引。我尝试使用df['0']['id']df['0','id']访问列,但两者都会引发错误..... 任何帮助将不胜感激! Thanks!谢谢!

    df
    id   name    ON    OFF    
    1111 foo   1673  17892
    1132 bar   1132  61730
    4636 zoo   2323   8491
    7711 baz   3958   3432

To get your desired output df = df.reset_index().set_index('id', drop = True).rename(columns = {'-1': 'ON', '0': 'OFF'})得到你想要的 output df = df.reset_index().set_index('id', drop = True).rename(columns = {'-1': 'ON', '0': 'OFF'})

For reference, if you want to access a multiindex, you have to use tuples and df.loc .作为参考,如果你想访问多索引,你必须使用元组和df.loc You can use print(df.index) to see how the indices are written.您可以使用print(df.index)查看索引的写入方式。 For example, df.loc[(1111, 'foo'), '-1'] gets the value with id: 1111, name: foo at column '-1'.例如, df.loc[(1111, 'foo'), '-1']在 '-1' 列获取 id: 1111, name: foo 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM