Reputation: 5693
I have a hierarchically indexed data frame:
>>> import pandas as pd
>>> df = pd.DataFrame(np.arange(4),
index=[['John', 'John', 'Vicki', 'Vicki'],
['a','b', 'a','b']],
columns=['score'])
score
John a 0
b 1
Vicki a 2
b 3
and a series with index identical to the first index level in the above data frame:
>>> series = pd.Series([100, 200], index=['John', 'Vicki'])
John 100
Vicki 200
Now I want to merge the data frame with the series, such that the values from the series are broadcasted along the second level index. The resulting data frame should look like this:
score salary
John a 0 100
b 1 100
Vicki a 2 200
b 3 200
How can I achieve that in pandas?
Upvotes: 2
Views: 351
Reputation: 16960
This should work:
df['salary'] = series.reindex(df.index, level=0)
Upvotes: 2