Problem: Attempting to filter one dataframe using a mask that compares its index to the index of another dataframe. I've created a boolean mask, but my attempts return only an empty dataset.
Question: Is there a way to mask using indexes, or perhaps by converting the indexes to columns and comparing the dataframes?
The mask looks like:
my_towns.set_index(['State', 'RegionName'], inplace=True)
index1 = my_towns.index
index2 = qtr_data.index
qtr_data[qtr_data.index.isin(my_towns.index)]
The result is an empty dataframe.
Test datasets my_towns and qtr_data. These dfs both have multi-index. A simple example could be created from the example below following the masking steps above, i.e, use df2.index as a filter on df1's rows.
df1 = pd.DataFrame({'State' : ['Hawaii', 'Alabama', 'California', 'Washington'], 'Region' : ['Honolulu', 'Mobile', 'Los Angeles', 'Spokane'], '2001q1' : ['123','345','456','567']}).set_index(['State','Region'])
df2 = pd.DataFrame({'State' : ['Alabama', 'California', 'Washington'], 'Region' : ['Mobile', 'Los Angeles', 'Spokane']}).set_index(['State', 'Region'])