ZhangZhihui's Blog  
import pandas as pd

df = pd.DataFrame([[2001, 'D', 2],
                   [2004, 'T', 3],
                   [2003, 'T', 5],
                   [2001, 'T', 4],
                   [2004, 'D', 7]],
                  columns=['year', 'type', 'cnt'])
df_year = df.groupby(['year', 'type']).sum('cnt')
print(df_year)
print(df_year.index)
print(df_year.loc[2001])              # Select first level
print(df_year.xs('T', level='type'))  # Select second level
print(df_year.loc[(2004, 'D')])

 

Output:

 

posted on 2022-01-07 22:14  ZhangZhihuiAAA  阅读(8)  评论(0编辑  收藏  举报