简体   繁体   中英

How to create a column depending on the row index values in a multiindex pandas dataframe?

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

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 ?

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

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

数据框

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM