1

I have a data frame that looks something like:

import pandas as pd
import numpy as np

f = {'business':['#','FX','IR'],
 'AL':['A','L','#'],
 'Company':['207','#','1']}
filterr = pd.DataFrame(data=f)
filterr

Whenever there is a '#' present in the dataframe, I need the rows to repeat based on every combination from a list.The set of list that I have looks something like:

business=['FX','IR']
AL=['A','L']
Company=['1','207']

So, The end result I am looking for looks something like:

f1 = {'business':['FX','IR','FX','FX','IR','IR'],
 'AL':['A','A','L','L','A','L'],
 'Company':['207','207','1','207','1','1']
 }

filter_output=pd.DataFrame(data=f1)
display(filter_output)

Any ideas on the most efficient way to do this ?

Thanks !!

2
  • updated code accordingly Commented Jun 24, 2019 at 17:31
  • 1
    yes. you are right. Modified the code accordingly Commented Jun 24, 2019 at 17:33

1 Answer 1

3

Here is one method , I am not sure about the efficient part

filterr.mask(filterr.eq('#')).fillna(d).stack().str.split(',').apply(pd.Series).stack().unstack(1).ffill()
Out[804]: 
    business AL Company
0 0       FX  A     207
  1       IR  A     207
  2       CR  A     207
1 0       FX  L       1
  1       FX  L     207
2 0       IR  A       1
  1       IR  L       1

d={'business':'FX,IR,CR',
'AL':'A,L',
'Company':'1,207'}
Sign up to request clarification or add additional context in comments.

1 Comment

@anky_91 Yep that could be a way too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.