I have this list of lists = [['Lost Summoner Kitty', 70], ['Ironbound', 4000]]
And I also have this dataframe:
I would like to find a general way to replace item_id integer by his name that is present in the previous list
CodePudding user response:
Turn the list of lists into a dictionary where the IDs are the keys. Then use that in pandas.Series.map()
to convert the item_id
columns.
mapping = dict(map(reversed, list_of_lists))
df.item_id = df.item_id.map(mapping)
CodePudding user response:
You can create a dict
from the list
and then use pandas.Series.map
on the created dict
.
lst = [['Lost Summoner Kitty', 70], ['Ironbound', 4000]]
dct = {num: entry for entry, num in lst}
# dct -> {70: 'Lost Summoner Kitty', 4000: 'Ironbound'}
df['item_id'] = df['item_id'].map(dct)
CodePudding user response:
You can use map
:
df['item_name'] = [dict([l[::-1] for l in lists]).get(i, None)
for i in df['item_id']]
Output :
print(df)
steam_id item_id item_name
0 XXX 70 Lost Summoner Kitty
1 YYY 4000 Ironbound