使用以下虚拟数据帧:
import pandas as pd
df = pd.DataFrame({'dt':['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-03', '2020-01-01', '2020-01-02', '2020-01-03', '2020-01-03'], 'group':['a', 'a', 'b', 'b', 'a', 'a', 'b', 'b'], 'bar':[1,2,3, 4, 1,2,3, 4], 'baz':[3,4,5, 6, 3,4,5, 6]})
df = df.groupby(['dt', 'group']).describe()
即我希望能够选择df[['dt', 'group', ('bar', '25%'), , ('bar', '25%'), , ('bar', '25%')]]
。
如何执行此多级选择?
我的最终目标是能够绘制出每个类别随时间变化的百分位数,其中每个类别都描述一种颜色:
import seaborn as sns; sns.set() sns.lineplot(data=df.reset_index()['baz'][['25%', '50%', '75%']], hue='group')
但是,在剩余的数据框中,没有有关组的信息。
使用以下虚拟数据框:以pd df = pd.DataFrame({'dt':['2020-01-01','2020-01-02','2020-01-03','2020 -01-03','2020-01-01','2020-01-02','2020-01-03','2020-01-03'],'group':['a',. 。
即我希望能够选择
df[['dt', 'group', ('bar', '25%'), , ('bar', '25%'), , ('bar', '25%')]]
。
不确定这是否是您想要的: