It seems my question is similar to a few others but I can't find one that fits my exact question, so apologies if I did not come across this particular answer if it already exists. This question was somewhat similar, but I don't want to assign a list as the new column value, I want to assign the name of the corresponding list as the column value.
For example, with a data frame such as this:
d = {'name': ['Tom', 'Patrick', 'Lamar'], 'rating': [100, 97, 95]}
df = pd.DataFrame(data=d)
print(df)
And a collection of lists such as this:
tier1 = ['Tom', 'Patrick']
tier2 = ['Lamar']
How do I efficiently create a new 'tier' column such as this:
d = {'name': ['Tom', 'Patrick', 'Lamar'], 'rating': [100, 97, 95], 'tier': [1, 1, 2]}
df = pd.DataFrame(data=d)
print(df)
My actual dataframe is ~300 rows and I have 11 tier lists. I originally approached this as a looping problem, but wasn't sure how to loop over the rows of the dataframe with pd.iterrows() by checking the 'name' column, finding what tier list it resides in, and then assigning the tier to the new column. I've seen similar types of problems where people make use of dictionaries instead of lists, so I converted my tier lists into a dictionary like below:
tier_dict = {
1: tier1,
2: tier2
}
print(tier_dict)
But I realized that most examples make use of the column variable matching a key in the dict and then assigning the value to the column, but in my case I want to assign the key as the new column value, and this is where I currently am. Thank you for any help!
In the next part create dictionary mapping to check name - tiers relation.
Last part use mapping to create new column
Output should be like that.