【发布时间】:2020-04-07 22:51:37
【问题描述】:
streamingHistory dateframe for reference我有一个包含我的 Spotify 数据的数据框和我的前 50 位最受欢迎的艺术家的列表。我想使用此列表查找每个相应的艺术家,而无需通过我的数据框 25 次。
# Find most popular artists from 2019
topFifty = streamingHistory[streamingHistory["year"] == 2019]["artistName"].value_counts().index[:50]
topFifty = streamingHistory[streamingHistory["artistName"] in top2019]
这段代码给了我一个类型错误
TypeError: 'Series' objects are mutable, thus they cannot be hashed
我现在有这个(可行),但我很好奇是否有办法在不应用辅助函数的情况下做到这一点......
topFifty = streamingHistory[streamingHistory["year"] == 2019]["artistName"].value_counts().index[:25]
def findArtists(row):
if (row["artistName"] in topFifty) & row["year"] == 2019:
return row
df = streamingHistory.apply(findArtists, axis=1).dropna().reset_index(drop=True)
【问题讨论】:
标签: python pandas function helper