考虑以下 DataFrame:import pandas as pdarrays = [['A','A','B','B','C','C'],[1,1,3,3,5,5,],[2,2,4,4,6,6],[0.1,0.2,0.3,0.4,0.5,0.6]]index = pd.MultiIndex.from_arrays(arrays,names=('Sa...
考虑以下DataFrame:
import pandas as pd
arrays = [['A','A','B','B','C','C'],[1,1,3,3,5,5,],[2,2,4,4,6,6],[0.1,0.2,0.3,0.4,0.5,0.6]]
index = pd.MultiIndex.from_arrays(arrays,names=('Sample','P1','P2','T'))
data = np.random.rand(10,6)
df = pd.DataFrame(columns=index,data=data)
我想要选择:对于样本 A,选择 T=0.2 的列;对于样本 C,选择 T=0.5 的列。
我可以轻松选择每一列,例如:
df.loc[:,('A',slice(None),slice(None),0.2)] # or
df.loc(axis=1)[('C',slice(None),slice(None),0.5)]
但是我该如何将它们结合起来呢?我尝试提供一个元组列表:
df.loc[:,[('A',slice(None),slice(None),0.2),('C',slice(None),slice(None),0.5)]]
但这会产生错误。
我怎样才能选择我的列而不诉诸 pd.concat
?