In my Pandas query(), I want to filter based on whether one field is in a set found in a dict lookup on another field. Here is some stripped down code illustrating my problem:
import pandas
cars = {
'Japan': {'Honda', 'Nissan', 'Toyota'},
'USA': {'Ford', 'Chevrolet', 'Tesla'},
}
data = pandas.DataFrame({
'country': ['Japan', 'Japan', 'USA'],
'make': ['Honda', 'Ford', 'Ford'],
'date': ['2018-10-04', '2018-10-05', '2018-10-06'],
}).astype(dtype={'country': 'category', 'make': 'category', 'date': 'category'})
print data.query('make in @cars[country]')
I want to only include rows for which one field (make
) is present in the set found in a dict lookup based on another field (@cars[country]
).
Expected
country date make
0 Japan 2018-10-04 Honda
1 USA 2018-10-06 Ford
Actual
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Anyone have any idea how to make this work? Or do I need to resort to using apply
for this (which I understand is slower)?