计算满足两个条件的 Dataframe 列的元素

时间:2021-06-04 14:28:36

标签: python dataframe

Barcelona_venues 是一个包含两列的 Pandas 数据框(列 Neighborhood 具有邻域值,列 Venue Category 具有场地类别值)。两者都包含字符串值。

我想知道 Barcelona_venues 中的每个街区,interesting_venues 列表中包含的场地数量。

我收到以下错误:

<块引用>

ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。


interesting_venues = ["Café", "Bar", "Market", "Pharmacy", "Train Station", "Supermarket", "Convenience Store", "Flower Shop", "Cafeteria", "Metro Station"]

lst = []

Neigh = Barcelona_venues["Neighborhood"].unique()

for i in Neigh:
    
    count = ((Barcelona_venues[Barcelona_venues["Venue Category"] in interesting_venues]) and (Barcelona_venues[Barcelona_venues["Neighborhood"] == i]))
    lst.append[count]

lst

1 个答案:

答案 0 :(得分:0)

你可以试试:

interesting_venues = ["Café", "Bar", "Market", "Pharmacy", "Train Station", "Supermarket", "Convenience Store", "Flower Shop", "Cafeteria", "Metro Station"]
lst = []
Neigh = Barcelona_venues["Neighborhood"].unique()

# Loop through each neighborhood
for i in Neigh:
  count = 0
  # Loop through all venue categories for each neighborhood
  for j in Barcelona_venues[Barcelona_venues["Neighborhood"]==i]["Venue Category"]:
    # If it's an interesting category 
    if j in interesting_venues:
      count++
  lst.append[count]