TheTank
TheTank

Reputation: 505

How to use pandas rows to form new columns

I have the following:

pa = pd.DataFrame({'a':np.array([[1.,4.],[2.],[3.,4.,5.]]), 
                   'b':np.array([[2.,5.],[3., 6.],[4.,5.,6.]])})

This will yield:

    a               b
0   [1.0, 4.0]      [2.0, 5.0]
1   [2.0, 3.3]      [3.0, 6.0]
2   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0]

I have tried various techniques to concatenate items of each array into a new array.

Something in this fashion:

    a               b               c
0   [1.0, 4.0]      [2.0, 5.0]      [1.0, 2.0]
1   [1.0, 4.0]      [2.0, 5.0]      [4.0, 5.0]
2   [2.0, 3.3]      [3.0, 6.0]      [2.0, 3.0]
3   [2.0, 3.3]      [3.0, 6.0]      [3.3, 6.0]
4   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] [3.0, 4.0]
5   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] [4.0, 5.0]
6   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] [5.0, 6.0]

if there are other columns I can update those items into the newly created columns. But I'm stuck in getting to this position.

Can anyone please help out?

Upvotes: 1

Views: 49

Answers (2)

BENY
BENY

Reputation: 323226

By using zip with unnesting method

pa['New']=[list(zip(x,y)) for x, y in zip(pa.a,pa.b)]
s=pa.New.str.len()
df=pd.DataFrame({'a':pa['a'].repeat(s),'b':pa['b'].repeat(s),'New':list(map(list,pa.New.sum()))})
df
          New                a                b
0  [1.0, 2.0]       [1.0, 4.0]       [2.0, 5.0]
0  [4.0, 5.0]       [1.0, 4.0]       [2.0, 5.0]
1  [2.0, 3.0]       [2.0, 3.3]       [3.0, 6.0]
1  [3.3, 6.0]       [2.0, 3.3]       [3.0, 6.0]
2  [3.0, 4.0]  [3.0, 4.0, 5.0]  [4.0, 5.0, 6.0]
2  [4.0, 5.0]  [3.0, 4.0, 5.0]  [4.0, 5.0, 6.0]
2  [5.0, 6.0]  [3.0, 4.0, 5.0]  [4.0, 5.0, 6.0]

Upvotes: 2

rafaelc
rafaelc

Reputation: 59274

IIUC, you need something like this?

def f(row):
    return pd.Series(zip(row["a"], row["b"]))

mod = df.apply(f, 1).stack()
mod.index = mod.index.get_level_values(0)

df.merge(mod.to_frame(), left_index=True, right_index=True)

    a               b            c
0   [1.0, 4.0]  [2.0, 5.0]  (1.0, 2.0)
0   [1.0, 4.0]  [2.0, 5.0]  (4.0, 5.0)
1   [2.0, 3.3]  [3.0, 6.0]  (2.0, 3.0)
1   [2.0, 3.3]  [3.0, 6.0]  (3.3, 6.0)
2   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] (3.0, 4.0)
2   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] (4.0, 5.0)
2   [3.0, 4.0, 5.0] [4.0, 5.0, 6.0] (5.0, 6.0)

Upvotes: 0

Related Questions