Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 329
danche Avatar asked Dec 13 '22 21:12

danche


2 Answers

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
like image 169
cs95 Avatar answered Dec 16 '22 09:12

cs95


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

like image 22
piRSquared Avatar answered Dec 16 '22 10:12

piRSquared