如何更改或访问大 pandas MultiIndex列标题? [英] How do I change or access pandas MultiIndex column headers?
问题描述
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 '])
其中看起来像这样:
>>> df
id id0 id1 id2
lat 66 67 68
lon 110 111 112
2012 24.0 25.0 26.0
2013 28.0 28.0 29.0
2014 30.0 31.0 32.0
我想要调整 df的纬度或经度[ 'id0']
或 plot(df.ix [2014])
但在(x,y) code>基于
(lon,lat)的位置
。
您可以使用 df.columns.get_level_values('lat')
以获取索引对象。这将返回索引的副本,因此您无法扩展此方法来修改坐标。
但是,您可以直接访问级别并使用此解决方法对其进行修改。
导入熊猫为pd
导入numpy为np
df = pd.DataFrame (column = [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
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 (lon,lat) location from the header).
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'])
Which then looks like this:
>>> df
id id0 id1 id2
lat 66 67 68
lon 110 111 112
2012 24.0 25.0 26.0
2013 28.0 28.0 29.0
2014 30.0 31.0 32.0
I'd like to be able to adjust the latitude or longitude for df['id0']
, or plot(df.ix[2014])
but at (x,y)
location based on (lon,lat)
.
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
这篇关于如何更改或访问大 pandas MultiIndex列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!