Python 熊猫:带条件的多索引
我有一个多索引数据帧,ID_1和ID_2是我的索引:Python 熊猫:带条件的多索引,python,pandas,dataframe,multi-index,Python,Pandas,Dataframe,Multi Index,我有一个多索引数据帧,ID_1和ID_2是我的索引: ID_1 ID_2 feature_1 feature_2 1 1 0 0 2 1 1 2 1 1 1 2 0 1 我想要得到的是ID_1=1和feature_2=1的数据 即: ID_2 feature_1 feature_2 2 1 1 执行此操作
ID_1 ID_2 feature_1 feature_2
1 1 0 0
2 1 1
2 1 1 1
2 0 1
我想要得到的是ID_1=1和feature_2=1的数据
即:
ID_2 feature_1 feature_2
2 1 1
执行此操作的最佳方法是什么?对一行数据帧使用双[]元组:
df1 = df.loc[[(1,2)]]
或:
如果仅使用一个[]获取序列:
df1 = df.loc[[pd.IndexSlice[1,2]]]
print (df1)
feature_1 feature_2
ID_1 ID_2
1 2 1 1
s = df.loc[(1,2)]
print (s)
feature_1 1
feature_2 1
Name: (1, 2), dtype: int64