danche
danche

Reputation: 1815

How to insert n DataFrame to another every nth row in Pandas?

For example, I have an DataFrame A as following

A
0
1
2

Now I want to insert every 2 rows in DataFrame B into A every 1 row and B is as following

B
3
3
4
4
5
5

finally I want

A
0
3
3
1
4
4
2
5
5

How can I achieve this?

Upvotes: 4

Views: 2348

Answers (4)

Anton vBR
Anton vBR

Reputation: 18916

If we first split a to len(a) arrays and b to len(b) two arrays we can zip them together, stack and concatenate.

a = np.split(dfa.A.values,len(dfa.A))
b = np.split(dfb.B.values,len(dfb.B)/2)

c = np.concatenate(np.hstack(list(zip(a,b))))

pd.Series(c)

Returns:

0    0
1    3
2    3
3    1
4    4
5    4
6    2
7    5
8    5
dtype: int64

Upvotes: 0

piRSquared
piRSquared

Reputation: 294488

Setup
Consider the dataframes a and b

a = pd.DataFrame(dict(A=range(3)))
b = pd.DataFrame(dict(B=np.arange(3).repeat(2) + 3))

Solution
Use interleave from toolz or cytoolz
The trick is to split b into two arguments of interleave

from cytoolz import interleave

pd.Series(list(interleave([a.A, b.B[::2], b.B[1::2]])))

0    0
1    3
2    3
3    1
4    4
5    4
6    2
7    5
8    5
dtype: int64

This is a modification of @root's answer to my question

Upvotes: 4

BENY
BENY

Reputation: 323326

Maybe this one ?

A=len(df1)+len(df2)
df1.index=(list(range(0, A,3)))
df2.index=list(set(range(0, A))-set(range(0, A,3)))
df2.columns=['A']
df=pd.concat([df1,df2],axis=0).sort_index()

df
Out[188]: 
   A
0  0
1  3
2  3
3  1
4  4
5  4
6  2
7  5
8  5

Upvotes: 2

cs95
cs95

Reputation: 402814

One option is to take each dataframe's values, reshape, concatenate with np.hstack and then assign to a new dataframe.

In [533]: pd.DataFrame(np.hstack((df1.A.values.reshape(-1, 1),\
                                  df2.B.values.reshape(-1, 2))).reshape(-1, ),\
                       columns=['A'])
Out[533]: 
   A
0  0
1  3
2  3
3  1
4  4
5  4
6  2
7  5
8  5

Another solution with pd.concat and df.stack:

In [622]: pd.DataFrame(pd.concat([df1.A, pd.DataFrame(df2.B.values.reshape(-1, 2))], axis=1)\
                             .stack().reset_index(drop=True),\
                      columns=['A'])
Out[622]: 
   A
0  0
1  3
2  3
3  1
4  4
5  4
6  2
7  5
8  5

Upvotes: 6

Related Questions