JPC JPC - 1 year ago 93
Python Question

How to merge dataframe into another dataframe that created from product of first dataframe in pandas python?

I have a dataframe with column A,B then I have function foo(B) taking parameter B from the first dataframe returning number of rows based on value of B ,

import pandas as pd
import numpy as np
x = pd.DataFrame.from_items([('A', [1,1, 2, 3]), ('B', [4,3, 5, 6])])
def foo(B):
sX = pd.Series(np.random.randn(B))
sY = pd.Series(np.random.randn(B))
return pd.DataFrame.from_items([('X', sX.tolist()), ('Y', sY.tolist())])

I ultimately want to create a new dataframe that merge the two table.

enter image description here
enter image description here

I want my output to look like this below ...
enter image description here

Answer Source

Group data frame x by columns A and B, then generate data frame for each group using apply method:

(x.groupby(['A', 'B']).apply(lambda g: foo(g.B))
  .reset_index(level=2, drop=True).reset_index())

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download