1insert_index = 0
2insert_colname = 'new column'
3insert_values = [1, 2, 3, 4, 5] # this can be a numpy array too
4df.insert(loc=insert_index, column=insert_colname, value=insert_values)
1df = pd.DataFrame({'B': [1, 2, 3], 'C': [4, 5, 6]})
2
3df
4Out:
5 B C
60 1 4
71 2 5
82 3 6
9
10idx = 0
11new_col = [7, 8, 9] # can be a list, a Series, an array or a scalar
12df.insert(loc=idx, column='A', value=new_col)
13
14df
15Out:
16 A B C
170 7 1 4
181 8 2 5
192 9 3 6