0

I have a simple DataFrame with some text values, and I want to replace them with numerical values from a lookup DataFrame:

df = pd.DataFrame([['Mary', 'good', 7.89], ['Tom', 'poor', 4.41], 
               ['Anthony', 'good', 7.94], ['Samuel', 'average', 6.21],
               ['Alex', 'best', 8.87]],
              columns=('Athlete', 'performance', 'time'))

lookup_df = pd.DataFrame([['bad',1],['poor',2],['average',3],
                         ['good',4],['best',5]],
                        columns=('performance_id','vote'))

enter image description here

I want to replace the performance column with the vote column from the lookup_df DataFrame, so that the result DataFrame look like this:

enter image description here

I have tried different Pandas functions, but I always got some kind of error: Can only compare identically-labeled Series objects

df['vote'] = entry_lookup[lookup_df ['performance_id'] == df['performance']]['vote']

or

df['vote'] = df.lookup(df['performance'], lookup_df ['performance_id'])
Nicola Lepetit
  • 756
  • 8
  • 25
  • 1
    `df.merge(lookup_df,left_on='performance', right_on='performance_id',how='inner').drop(columns=['performance','performance_id'])` – sayan dasgupta Sep 12 '22 at 05:48

0 Answers0