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'))
I want to replace the performance
column with the vote
column from the lookup_df
DataFrame, so that the result DataFrame look like this:
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'])