通过第二索引通过多索引访问熊猫分组

时间:2018-07-31 10:28:36

标签: python pandas indexing pandas-groupby multi-index

找不到类似的问题。 假设我有一个带有Multiindex(城市,月份)的 grouped_price ,如下所示:

City  Month     Price Sales 
LA   2017-01     10    10
     2017-02     15    20
     2017-05     20    35       
     2017-07     25    40
NY   2017-01     10     5
     2017-03     15    30
     2017-05     20    40       
     2017-06     25    45
CH   2017-01     7     10
     2017-02     11    22
     2017-07     30    41
OL   2017-01     9     10
     2017-02     17    10
     2017-05     20    30       
     2017-07     25    41 
     2017-08     30    47

所以对于“正常”循环订单,城市->我做了一个月:

Cities = grouped_price.index.levels[0]
for city in Cities:
    labels = grouped_price.loc[city].index.labels
    levels = grouped_price.loc[city].index.levels
    Months = levels[0][labels[0]].unique() # for each City get a list of existing Months
    for mon in Months:
        # do things here
        x = grouped_price.loc[city, mon]  # ERROR here!

,并且有效。但是对于反向循环:

Months = grouped_price.index.levels[1]
Cities = grouped_price.index.levels[0]
for mon in Months:
    # Here I should get the list of Cities for specific Month
    for city in Cities:
        # do things here
        x = grouped_price.loc[city, mon]  # ERROR here!

给出一个错误,因为并非所有的City-mon对都存在于MultiIndex中。 我应该找到特定月份的城市列表,该列表存在一对,但我不知道如何。

Cities = grouped_price.loc[:, mon] - doesn't work

P.S。我知道我可以旋转表,或以相反的顺序将它们分组,但我不想这么做。

1 个答案:

答案 0 :(得分:1)

一种解决方案是颠倒MultiIndex级别的顺序:

df = df.swaplevel(0, 1)

(可选)您可能还希望对新的MultiIndex进行排序。这是一个最小的示例:

df = pd.DataFrame([[0, 1, 2], [0, 2, 3], [1, 3, 4], [1, 1, 5]],
                  columns=['idx1', 'idx2', 'col'])

df = df.set_index(['idx1', 'idx2'])
df = df.swaplevel(0, 1).sort_index()

print(df)

idx2 idx1     
1    0       2
     1       5
2    0       3
3    1       4
相关问题