访问 loc 中的列值会产生错误"'Series' objects are mutable, thus they cannot be hashed"



i有一个带有两个感兴趣的stationhall列的数据框,其中HALL具有多个站点。我想过滤数据框,以便我只保留每个大厅的某些站点。

因此,对于数据框中的每一行,仅在hall == A and station in [A1, A2, A3] or hall == B and station in [B1, B3, B5] or ....

时保留它

为例,如果这是起始DF

Date   Meal   Hall     Station      Food
04/10  lunch  de neve  the grill    pizza
04/10  lunch  de neve  the kitchen  burger
04/10  lunch  covel    the oven     hot dog
04/10  lunch  covel    the kitchen  pasta

我只想将the grill保留为de neve和the oven的COVEL,我应该以

结束
Date   Meal   Hall     Station      Food
04/10  lunch  de neve  the grill    pizza
04/10  lunch  covel    the oven     hot dog

我创建了一个词典,该字典捕获了我要保留的每个大厅的电台

essential_stations = 
    {'HallA': [station1, station2],
     'HallB': [station3, station4],
     'HallC': [station5, station6],
    }

但是,当我执行以下

summary_food_df = food_df[food_df['station'].isin(essential_stations[food_df['hall']])]

我得到错误

'Series' objects are mutable, thus they cannot be hashed

有其他方法可以使用字典过滤额外的站点?

df = pd.DataFrame({'Date': {0: '04/10', 1: '04/10', 2: '04/10', 3: '04/10'},
 'Food': {0: 'pizza', 1: 'burger', 2: ' hot dog', 3: 'pasta'},
 'Hall': {0: 'de neve', 1: 'de neve', 2: 'covel', 3: 'covel'},
 'Meal': {0: 'lunch', 1: 'lunch', 2: 'lunch', 3: 'lunch'},
 'Station': {0: 'the grill',
  1: 'the kitchen',
  2: 'the oven',
  3: 'the kitchen'}})
essential_stations = {'de neve': ['the grill'], 'covel': ['the oven']}
# user apply to filter rows which satisfiy the condition (only certain stations for a hall)
df.apply(lambda x: x if x.Station in essential_stations.get(x.Hall) else np.nan, axis=1).dropna()
Out[265]: 
    Date      Food     Hall   Meal    Station
0  04/10     pizza  de neve  lunch  the grill
2  04/10   hot dog    covel  lunch   the oven

相关内容

最新更新