Home > Blockchain >  Append element to column with numpy.ndarray datatype in Pandas
Append element to column with numpy.ndarray datatype in Pandas

Time:01-11

I want to append a string to a column which is a numpy.ndarray object.The following code is not working:

def filter_by_player(df, players, team):
    filtered_df = df[df['player'].isin(players)]
    filtered_df['league'] = filtered_df['league'].apply(lambda x: x   [team])
    return filtered_df

the league column looks like this ['barca','real','sevilla']. I want to add to it but the code above is not working.

players = ['messi', 'benzema', 'busquets']

league_df
player      | team
messi       | ['barca']
lewandowski | ['dortmund', 'bayern', 'barca']

when i call the function filter_by_player(players, 'psg')

the new dataframe league_df should become this as messi is in the list of players:

player      | team
messi       | ['barca', 'psg']

CodePudding user response:

IIUC, each of the elements in the team or league column is a numpy array. So, use np.append instead of appending lists. Try this -

import numpy as np

def filter_by_player(df, players, team):
    filtered_df = df[df['player'].isin(players)]
    filtered_df['team'] = filtered_df['team'].apply(np.append, args=([team]))
    return filtered_df

new_df = filter_by_player(league_df, ['messi'], 'psg')
print(new_df)
  player          team
0  messi  [barca, psg]

Just to double check if the initial datatype and post datatypes are numpy -

print(type(league_df.iloc[0,1]))
print(type(new_df.iloc[0,1]))
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

EDIT: If you arent not strict about keeping it as a numpy array, try this -

def filter_by_player(df, players, team):
    filtered_df = df[df['player'].isin(players)]
    filtered_df['league'] = filtered_df['league'].apply(lambda x: list(x) [team])
    return filtered_df

new_df = filter_by_player(league_df, ['messi'], 'psg')
print(new_df)
print(type(league_df.iloc[0,1]))
print(type(new_df.iloc[0,1]))
  player        league
0  messi  [barca, psg]

<class 'numpy.ndarray'>
<class 'list'>
  • Related