Select 数据来自 pandas 多索引数据透视表

Select data from pandas multiindex pivottable

我有一个包含 1703 行的多索引数据框(可透视),如下所示:

Local code     Ex Code    ...  Value      
159605         FR1xx      ...  30               
159973         FR1xx      ...  50    
...
ZZC923HDV906   XYxx       ...  20

有数字本地代码(例如159973)或由字符和字符串组成的本地代码(例如ZZC923HDV906) 我想 select 第一个索引列(本地代码)的数据。这适用于具有以下代码的字符串字符

pv_comb[(pv_comb.index.get_level_values("Local code") == "ZZC923HDV906")] 

但是我无法select数值:

pv_comb[(pv_comb.index.get_level_values("Local code") == 159973)]

此 returns 一个空数据框。 是否可以将多索引第一列的值转换为字符串字符,然后 select 数据?

IIUC 你需要 '',因为你的 numeric 值是 strings - 所以 159973 改为 '159973':

pv_comb[(pv_comb.index.get_level_values("Local code") == '159973')]

如果需要将 MultiIndex 的某个级别转换为 string 需要创建新索引然后分配:

#if python 3 add list
new_index = list(zip(df.index.get_level_values('Local code').astype(str),
                df.index.get_level_values('Ex Code')))

df.index = pd.MultiIndex.from_tuples(new_index, names = df.index.names)

也有可能有一些whitespaces,你可以通过strip:

删除它们
#change multiindex
new_index = zip(df.index.get_level_values('Local code').astype(str).str.strip(),
                df.index.get_level_values('Ex Code')
df.index = pd.MultiIndex.from_tuples(new_index, names = df.index.names)

解决办法如果很多层是第reset_problematic层,做操作,把索引设回去。那么有可能sortlevel是必须的:

df = pd.DataFrame({'Local code':[159605,159973,'ZZC923HDV906'],
                   'Ex Code':['FR1xx','FR1xx','XYxx'],
                   'Value':[30,50,20]})
pv_comb = df.set_index(['Local code','Ex Code'])
print (pv_comb)
                      Value
Local code   Ex Code       
159605       FR1xx       30
159973       FR1xx       50
ZZC923HDV906 XYxx        20

pv_comb = pv_comb.reset_index('Local code')
pv_comb['Local code'] = pv_comb['Local code'].astype(str)
pv_comb = pv_comb.set_index('Local code', append=True).swaplevel(0,1)
print (pv_comb)
                      Value
Local code   Ex Code       
159605       FR1xx       30
159973       FR1xx       50
ZZC923HDV906 XYxx        20