Home > Enterprise >  KeyError when applying a function to all rows in a data frame
KeyError when applying a function to all rows in a data frame

Time:04-11

I have defined a function as below:

def song_words(row):
    # Extracts the song name from the row
    song_name_raw = row['Song'][0]
    # Extracts the song's points from the row
    song_points = row['Points'][0]

. . .

The function basically associates a word in a song and the number of points for it, given in a tuple. The output looks like below: [('Net', 31), ('als', 31), ('toen', 31)]

The above function is only applied to a single row, i.e. row['Song'][0] and row['Points'][0]

How can I apply the song_words() function to all rows in my dataframe for the two relevant columns (Song, Points)?

I have tried the following ways, but it doesn't work.

 data.groupby('Song').apply(song_words)
 data.apply(song_words)

any advice appreciated :)

CodePudding user response:

If you want to apply to rows, you need to write:

data.apply(song_words, axis=1)

Otherwise, apply applies the function to a column at a time.

CodePudding user response:

I think this is what you are looking for.

def song_words(row):
    # extract song name
    print(row['songs'])
    # extract song points
    print(row['points'])

data = {'songs': ['s1', 's2'], 'points': [10, 9]}
df = pd.DataFrame(data)
df

df.apply(song_words, axis=1)
  • Related