Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join series with repeated index on dataframe where column values are equal to the index in the series

Say I have the series an dataframe like:

import pandas as pd
s = pd.Series([10,20,11,12,30,34],
    index=["red","red","blue","blue","green","green"])
s.index.name="numbers"

df = pd.DataFrame({
    "color":["red","green","blue","blue","red","green"],
    "id":[1,2,3,4,5,6]})

I want to add the values in s to the column in df in the same order as they appear where the index of s is equal to df["color"] i.e

pd.some_function(df,s,left_on="color",right_index=True)

color   id    numbers
red      1      10
green    2      30
blue     3      11
blue     4      12
red      5      20
green    6      34

I have tried pd.merge, pd.join etc. but I simply cannot make it work (without looping over df, filtered by color, add the data from s and then concat it at the end)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use groupby.cumcount to set up a unique key for the merge:

idx1 = s.groupby(level=0).cumcount()
# [0, 1, 0, 1, 0, 1]
idx2 = df.groupby('color').cumcount()
# [0, 0, 0, 1, 1, 1]

s.index.name="color"
out = (df
   .merge(s.reset_index(name='number'),
          left_on=['color', idx2], right_on=['color', idx1])
   .drop(columns='key_1')
)

variant:

s.index.name="color"
out = (df
   .assign(idx=df.groupby('color').cumcount())
   .merge(s.reset_index(name='number')
           .assign(idx=s.groupby(level=0).cumcount().values),
          left_on=['color', 'idx'], right_on=['color', 'idx'])
    .drop(columns='idx')
)

output:

   color  id  number
0    red   1      10
1  green   2      30
2   blue   3      11
3   blue   4      12
4    red   5      20
5  green   6      34
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading