Reputation: 23
I have a DataFrame 'songDataFrame' with information about songs. There are three columns: 'artist', 'title', 'genres'. The first two are string and 'genres' contains lists of genres. It looks like this:
id artist title genres
1 'Sarah Bareilles' 'Opening Up' ['acoustic','pop', 'piano']
2 'Post Malone' 'Stay' ['Hip-Hop', 'rap', 'trap']
3 'Dean Lewis' 'Waves' ['alternative', 'guitar', 'indie']
4 'Billie Eilish' 'Watch' ['indie', 'indie pop', 'pop']
5 'Passenger' 'Let Her Go' ['metal', 'screamo', 'gore']
Genres for artist 'Passenger' are wrong. I want to change the existing list to ['indie rock', 'folk rock'].
If it wasn't a list, but I was replacing with a string I would just do:
mask = songDataFrame.artist =='Passenger'
songDataFrame.loc[mask, 'genres'] = 'indie folk'
This way it works fine. When instead of 'indie folk' I put ['indie rock', 'folk rock'],
mask = songDataFrame.artist =='Passenger'
songDataFrame.loc[mask, 'genres'] = ['indie folk','folk rock']
I get this error:
ValueError: Must have equal len keys and value when setting with an iterable
Is there a way to solve that?
Upvotes: 1
Views: 67
Reputation: 1258
Without the extra package and the literal_eval
trick:
mask = (songDataFrame.artist == 'Passenger').tolist().index(True)
songDataFrame.at[mask, 'genres'] = ['indie folk','folk rock']
Upvotes: 0
Reputation: 71610
Use two-lines and use loc
for assigning a string, make all column values string, then make them back to list:
import ast
songDataFrame.loc[songDataFrame['artist']=='Passenger','genres']="['indie folk','folk rock']"
songDataFrame['genres']=songDataFrame['genres'].astype(str).apply(ast.literal_eval)
And now:
print(songDataFrame)
Is:
id artist title genres
0 1 Sarah Bareilles Opening Up [acoustic, pop, piano]
1 2 Post Malone Stay [Hip-Hop, rap, trap]
2 3 Dean Lewis Waves [alternative, guitar, indie]
3 4 Billie Eilish Watch [indie, indie pop, pop]
4 5 Passenger Let Her Go [indie folk, folk rock]
Upvotes: 2