如何将Pandas数据框值与列表进行比较,并将结果放在新列中

时间:2018-11-19 19:48:19

标签: python pandas

对于曼哈顿街区,我有以下数据集,每个街区中最常见的场所:

df

我列出了地点清单:

fit_venues = ['Coffee Shop', 'Café', 'Park', 'Hotel', 'Sandwich Place', 'Pizza Place', 'Gym / Fitness Center', 'Exhibit', 'Gym', 'Supermarket', 'Nightclub', 'Concert Hall', 'Jazz Club']

,我想在数据框中添加一列(例如,我们称其为“适合邻里”),然后将每个邻里最常见的场所(5列)与列表“ fit_venues”进行比较。然后,将结果分配到“适合邻居”列(是/否或正确/错误)。例如,前两行应返回Yes / True,第三行应返回No / False。

有帮助吗?

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用DataFrame.isin()

您没有给我提供最常见的场所列的名称,因此我假设它们是DataFrame(df)中唯一的列:

fit_venues = ['Coffee Shop', 'Café', 'Park', 'Hotel', 'Sandwich Place', 'Pizza Place', 'Gym / Fitness Center', 'Exhibit', 'Gym', 'Supermarket', 'Nightclub', 'Concert Hall', 'Jazz Club']

df['Fit Neighborhood'] = df.isin(fit_venues).any()

答案 1 :(得分:0)

查看是否可行:

 fit_venues = ['Coffee Shop', 'Café', 'Park', 'Hotel', 'Sandwich Place', 'Pizza Place', 'Gym / Fitness Center', 'Exhibit', 'Gym', 'Supermarket', 'Nightclub', 'Concert Hall', 'Jazz Club']

df["binary_check"] = df[df["5th Most Common Venue"].isin(fit_venues)]