2
votes

I have a Pandas Dataframe such as:

  Column_1   Column_2   Column_3   Column_4
0.   a          d          d1         d2
1.   b          e          e1         e2

and I need to make all possible combinations of Column_1 and Column_2 such that Column_3 and Column_4's values remain the same as the row that contains Column_2. This should be the output:

  Column_1   Column_2   Column_3   Column_4
0.   a          d          d1         d2
1.   a          e          e1         e2
2.   b          d          d1         d2
3.   b          e          e1         e2

Can anybody help me out with this?

1
Is Column_1 actually relevant for the dataframe? It seems like what you really need is column 1 (could be a separate Series) combined with all the entries from the DF that is Columns 2-4, did I get that correctly? - Tacratis

1 Answers

0
votes

I think you are looking for reindex with multiple index from product

col=df.columns
df.set_index(['Column_1','Column_2'],inplace=True)
idx=pd.MultiIndex.from_product([df.index.get_level_values(0),df.index.get_level_values(1)])
df=df.reindex(idx,method = 'ffill').reset_index()
df.columns=col
df

  Column_1 Column_2 Column_3 Column_4
0        a        d       d1       d2
1        a        e       d1       d2
2        b        d       d1       d2
3        b        e       e1       e2