マルチインデックスで構成されるフィルターがあります。その多重指数に含まれている値にのみ条件を適用したいと思います。出来ますか?
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
# this multiindex comes from a model so it is fixed and we cannot know the values in advance
fixed_multiindex = pd.MultiIndex.from_tuples([('bar','one'), ('foo', 'one')])
df_multinidex_and_condition = df.loc[(fixed_multiindex) & (df[0] > -1.3)]
エラー
Output error TypeError: other must be a MultiIndex or a list of tuples
使用Index.isin
:
df_multinidex_and_condition = df.loc[df.index.isin(fixed_multiindex) & (df[0] > -1.3)]
print (df_multinidex_and_condition)
0 1 2 3
bar one -0.217679 0.821455 1.481278 1.331864
foo one 2.672172 0.464802 0.845930 -0.503542
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加