Python Forum
Using Pandas to store spotipy output in csv (python)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Pandas to store spotipy output in csv (python)
#1
Hi everyone,

I want to use spotipy to collect data from the spotify API.
I was able to make it work with:


import pandas as pd
import spotipy
sp = spotipy.Spotify()

from spotipy.oauth2 import SpotifyClientCredentials 

cid ='XXXCIDXXX' 
secret = 'XXXSECRETXXX' 
client_credentials_manager = SpotifyClientCredentials(client_id=cid, 
client_secret=secret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) 
sp.trace=False 

playlist = sp.user_playlist_tracks('spotify', '37i9dQZF1DX5nwnRMcdReF') 
songs = playlist['items']

df = pd.DataFrame(songs)

df.to_csv('Songs.csv', sep=';', encoding='utf-8', index=True)
This Outputs a a lot of data that i dont Need.
I found a code to only output the data that I need which is:

for i, item in enumerate(playlist['items']):
    track = item['track']
    need = (i, track['artists'][0]['name'], track['name'], track['id'])
now i can use print(need) to output exactly what I want, but I dont know how to store the data into the DataFrame.

If someone could help me that would be great.

Thank you.
Reply
#2
Try make a new Dataframe based on the loop.
Then write that Dataframe to csv new_df.to_csv
Untested example:
need = []
for i, item in enumerate(playlist['items']):
    track = item['track']
    need.append((i, track['artists'][0]['name'], track['name'], track['id']))

new_df = pd.DataFrame(need, columns=('Item', 'Artists', 'Name', 'Id'))
Reply
#3
Hi Snippsat,

It works. Thank you very much :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Output In Pandas DataFrame Query eddywinch82 1 1,903 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod( codeowl 2 2,163 Mar-28-2020, 08:27 AM
Last Post: j.crater
  Can't store pandas converted json dataframe into mongoDB mahmoud899 1 4,204 Dec-12-2018, 07:45 PM
Last Post: nilamo
  Output substrings from rows in pandas brocq_18 5 3,914 Jun-21-2018, 11:30 AM
Last Post: brocq_18
  Issue in getting the required data from nested json and store it in a pandas datafram PrateekG 2 3,278 May-20-2018, 11:25 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020