Home > Net >  How to insert a pre-initialized dataframe into another dataframe at a specified column position?
How to insert a pre-initialized dataframe into another dataframe at a specified column position?

Time:01-26

Suppose we have the following dataframe.

  col1 col2   col3
0  one  two  three
1  one  two  three
2  one  two  three
3  one  two  three
4  one  two  three

We seek to introduce 31 columns into this dataframe, each column representing a day in the month.

Let's say we want to introduce it precisely between columns col2 and col3.

How do we achieve this?

To make it simple, the introduced columns can be numbered from 1 to 31.

Starting source code

import pandas as pd

src = pd.DataFrame({'col1': ['one', 'one', 'one', 'one','one'],    
                    'col2': ['two', 'two', 'two', 'two','two'],    
                    'col3': ['three', 'three', 'three', 'three','three'],
                    })

CodePudding user response:

You can use res

CodePudding user response:

If your purpose is to add and initialize new columns, use reindex:

cols = list(src)
cols[2:2] = range(1,31 1)

df = src.reindex(columns=cols, fill_value=0)

Output:


  col1 col2  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31   col3
0  one  two  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  three
1  one  two  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  three
2  one  two  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  three
3  one  two  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  three
4  one  two  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  three

CodePudding user response:

Another possible solution:

pd.concat([src.iloc[:, :2].assign(
    **{str(col): 0 for col in range(1, 32)}), src['col3']], axis=1)

Output:

  col1 col2  1  2  3  4  5  6  7  8  ...  23  24  25  26  27  28  29  30  31  \
0  one  two  0  0  0  0  0  0  0  0  ...   0   0   0   0   0   0   0   0   0   
1  one  two  0  0  0  0  0  0  0  0  ...   0   0   0   0   0   0   0   0   0   
2  one  two  0  0  0  0  0  0  0  0  ...   0   0   0   0   0   0   0   0   0   
3  one  two  0  0  0  0  0  0  0  0  ...   0   0   0   0   0   0   0   0   0   
4  one  two  0  0  0  0  0  0  0  0  ...   0   0   0   0   0   0   0   0   0   

    col3  
0  three  
1  three  
2  three  
3  three  
4  three  

[5 rows x 34 columns]
  • Related