我有一個包含公司和申請期的多索引的系列。我想用一個單獨的布爾公司系列來索引這個系列,這些公司通過一些過濾器,回傳與提供的公司串列匹配的所有公司和提交周期。
這是示例代碼:首先創建一個具有多索引的系列。
arrays = [
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.Series(np.random.randn(8), index=index)
first second
bar one -0.107011
two 1.170427
baz one -0.567174
two -0.902847
foo one -0.986784
two 0.972722
qux one 0.714136
two 0.044249
dtype: float64
然后創建布爾系列(在我的情況下,從一個groupby物件生成,該物件帶有多個申請期的公司的過濾器):
test = pd.Series([True, False, True, False], index=['bar', 'baz', 'foo', 'qux'], name='first')
bar True
baz False
foo True
qux False
Name: first, dtype: bool
我想切片系列只回傳行相匹配的公司bar,并foo和忽略休息。
s.loc[test]
>>>IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
你能幫我嗎?
uj5u.com熱心網友回復:
嘗試:
>>> s[test[test].index]
first second
bar one -0.188867
two 0.025214
foo one 0.693238
two 2.569597
dtype: float64
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/392007.html
