我正在创建一个脚本,它采用艺术家的数据帧,将它们传递给 Spotify API 并返回一些额外的信息。我正在努力研究如何最好地将艺术家的数据帧迭代地传递给调用 Spotify 的函数。我有两个功能:artists = grabArtists()它返回艺术家列表,例如:0 Gas1 System3 Finlay Shakespeare4 Boards Of Canada6 Delia Derbyshire Appreciation Society然后我有功能抓取 Spotify 艺术家数据...def grabSpotifyData(searchQ):# ERASE CACHE AND PROMPT FOR USER PERMISSIONclient_credentials_manager = SpotifyClientCredentials()sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)# SEARCH FOR AN ARTISTsearchQuery = searchQsearch = sp.search(searchQuery, 1, 0, "artist")artist = search['artists']['items'][0]genres = artist['genres']image = artist['images'][0]['url']pop = artist['popularity']followers = artist['followers']['total']return genres, image, pop, followers我可以通过以下操作非常简单地获取单个结果的数据帧。我怎样才能进行这个迭代,以便它通过艺术家列表循环并附加关于艺术家的上下文?artists = grabArtists()[0]result = grabSpotifyData(artists)df = pd.DataFrame([(artists), (result[0]), (result[1]), (result[2]), (result[3])])df = df.T它应该返回一个数据框,其中的列是:艺术家、流派、图像、流行度、追随者。提前致谢!
2 回答
SMILET
TA贡献1796条经验 获得超4个赞
我认为您可以将艺术家作为列表迭代传递给 api:
df = []
for artist in artists:
result = grabSpotifyData(artist)
d0 = pd.DataFrame([(artists), (result[0]), (result[1]), (result[2], (result[3])])
df.append(d0)
df = pd.concat(df, axis=1)
LEATH
TA贡献1936条经验 获得超6个赞
df = pd.DataFrame()
for artist in artists:
try:
results = grabSpotifyData(artist)
except IndexError:
pass
df = df.append(results)
df = pd.DataFrame(df)
df = df.reset_index()
添加回答
举报
0/150
提交
取消