How do I change or access pandas MultiIndex column headers?

后端 未结 2 1419
独厮守ぢ
独厮守ぢ 2021-01-06 20:35

I have the following Pandas DataFrame, but am having trouble updating a column header value, or easily accessing the header values (for example, for plotting a time at the (

2条回答
  •  清酒与你
    2021-01-06 21:33

    You can use df.columns.get_level_values('lat') in order to get the index object. This returns a copy of the index, so you cannot extend this approach to modify the coordinates inplace.

    However, you can access the levels directly and modify them inplace using this workaround.

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(columns = ["id0", "id1", "id2"])
    df.loc[2012]= [24, 25, 26]
    df.loc[2013]= [28, 28, 29]
    df.loc[2014]= [30, 31, 32]
    
    df.columns = pd.MultiIndex.from_arrays([df.columns, [66,67,68], [110,111,112]],
                                           names=['id','lat','lon'])
    
    ids = df.columns.get_level_values('id')
    id_ = 'id0'
    column_position = np.where(ids.values == id_)
    
    new_lat = 90
    new_lon = 0
    
    df.columns._levels[1].values[column_position] = new_lat
    df.columns._levels[2].values[column_position] = new_lon
    

提交回复
热议问题