1

I would like to add the regional information to the main table that contains entity and account columns. In this way, each row in the main table should be duplicated, just like the append tool in Alteryx.

Is there a way to do this operation with Pandas in Python?

Thanks!

enter image description here

help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36
ba90343
  • 11
  • 1

1 Answers1

0

Unfortunately no build-in method exist, as you'll need to build cartesian product of those DataFrame check that fancy explanation of merge DataFrames in pandas

But for your specific problem, try this:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(columns=['Entity', 'Account'])
df1.Entity = ['Entity1', 'Entity1']
df1.Account = ['Sales', 'Cost']

df2 = pd.DataFrame(columns=['Region'])
df2.Region = ['North America', 'Europa', 'Asia']

def cartesian_product_simplified(left, right):
    la, lb = len(left), len(right)
    ia2, ib2 = np.broadcast_arrays(*np.ogrid[:la,:lb])

    return pd.DataFrame(
        np.column_stack([left.values[ia2.ravel()], right.values[ib2.ravel()]]))

resultdf = cartesian_product_simplified(df1, df2)
print(resultdf)

output:

         0      1              2
0  Entity1  Sales  North America
1  Entity1  Sales         Europa
2  Entity1  Sales           Asia
3  Entity1   Cost  North America
4  Entity1   Cost         Europa
5  Entity1   Cost           Asia

as expected.

Btw, please provide the Data Frame the next time as code, not as a screenshot or even as link. It helps up saving time (please check how to ask)

dboy
  • 1,004
  • 2
  • 16
  • 24