0

I have a variable, playlists, which I want to assign a categorical variable which takes values of "sleep" or "no_sleep", for each playlist. I have both lists with sleep / no sleep already assign as follows: Sleep: "Peaceful piano", "sleep", "Baby Sleep Aid", "White Noise"

No_sleep: "Dance Hits", "Dance Classics", "Massive Dance Hits", "Dance Party" : ("Spotify", "37i9dQZF1DXaXB8fQg7xif")

How do I add the new variable to the pd.dataframe and assign the values? I imagine an if function for sleep and everything else is No_sleep, but not sure how to get started.

mterrestre
  • 23
  • 3
  • 1
    provide a data sample , and expected output in addition to explanation, check [how to make a good pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky Jun 21 '20 at 16:41

1 Answers1

0

So the problem is to assign certain entries either no_sleep or sleep given the playlist name I am guessing.

Here is one way to do this:

df['sleep'] = "no_sleep"

sleep_idxs = np.array(df[df['playlist'].isin(sleep_playlists)].index)

df.iloc[sleep_idxs, 'sleep'] = "sleep"

Here you would need to have a list of those sleep playlist names (sleep_playlists).

shepan6
  • 134
  • 1
  • 5