Reputation: 6431
Let's suppose that you have the following dataframe:
ridx = pd.MultiIndex.from_tuples([('Tom','apple'),('Tom','banana'),('Anna','banana'),('Anna','citrus')], names=['basket', 'fruit'])
df = pd.DataFrame({'Quantity' : [1, 2, 3, 4]}, index=ridx)
df
And you have a dictionary that tell you how much sugar each fruit has:
sugar_content = {'apple':3,'banana':10,'citrus':2}
How do you add a new column to the dataframe that contains the sugar content of the fruit specified in the 'fruit' index ?
Upvotes: 1
Views: 75
Reputation: 210842
bit faster way to do this:
In [58]: df['sugar_content'] = df.reset_index()['fruit'].map(sugar_content).to_frame().set_index(df.index)
In [59]: df
Out[59]:
Quantity sugar_content
basket fruit
Tom apple 1 3
banana 2 10
Anna banana 3 10
citrus 4 2
Explanation:
In [60]: df.reset_index()['fruit'].map(sugar_content)
Out[60]:
0 3
1 10
2 10
3 2
Name: fruit, dtype: int64
In [61]: df.reset_index()['fruit'].map(sugar_content).to_frame()
Out[61]:
fruit
0 3
1 10
2 10
3 2
In [62]: df.reset_index()['fruit'].map(sugar_content).to_frame().set_index(df.index)
Out[62]:
fruit
basket fruit
Tom apple 3
banana 10
Anna banana 10
citrus 2
Upvotes: 1
Reputation: 6431
You can retrieve an array of index values with df.index.get_level_values()
and then use np.vectorize()
over the get method of the dictionary in order to perform the map:
fruits = df.index.get_level_values('fruit').values
fruits_sugar = np.vectorize(sugar_content.get)(fruits) # vectorize the get method of the dictionary and pass the sugar_content ndarray
df['sugar per fruit'] = fruits_sugar
df
Upvotes: 0