I am facing this issue, where I have one dataframe let's say df1
:
>>> df1 = pd.DataFrame(data = np.arange(45).reshape(-1,9), columns = ['A1','B1', 'C1',
'A2', 'B2', 'C2','A3','B3','C3'])
>>> df1
A1 B1 C1 A2 B2 C2 A3 B3 C3
0 0 1 2 3 4 5 6 7 8
1 9 10 11 12 13 14 15 16 17
2 18 19 20 21 22 23 24 25 26
3 27 28 29 30 31 32 33 34 35
4 36 37 38 39 40 41 42 43 44
And another dataframe df2
:
>>> df2 = pd.DataFrame(data = np.arange(15).reshape(-1,3), columns = ['AB1','AB2','AB3'])
>>> df2
AB1 AB2 AB3
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
4 12 13 14
Now what I want is, to insert columns of df2 into df1 in specific positions, so that df1 becomes (actually a new df would work too):
>>> df1
A1 AB1 B1 C1 A2 AB2 B2 C2 A3 AB3 B3 C3
0 0 0 1 2 3 1 4 5 6 2 7 8
1 9 3 10 11 12 4 13 14 15 5 16 17
2 18 6 19 20 21 7 22 23 24 8 25 26
3 27 9 28 29 30 10 31 32 33 11 34 35
4 36 12 37 38 39 13 40 41 42 14 43 44
I am now achieving this by creating a new empty df, then iterating over columns of both the df's, and then adding each column sequentially. Which is inefficient, ugly and defeats the whole purpose of DataFrames. So I would like to know is there already a method for this? I am not sure if such question has already been answered here, but I am sure I didn't find any results. If this has been discussed before, I will be glad if someone points me to it.