简体   繁体   中英

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

I initially had this 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:


    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. 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! 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'})

For reference, if you want to access a multiindex, you have to use tuples and df.loc . You can use print(df.index) to see how the indices are written. For example, df.loc[(1111, 'foo'), '-1'] gets the value with id: 1111, name: foo at column '-1'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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