I have a dataframe of 9,000 columns and 100 rows. I want to insert a column after every 3rd column such that its value is equal to 50 for all rows.
Existing DataFrame
  0 1 2 3 4 5 6 7 8 9....9000
0 a b c d e f g h i j ....x
1 k l m n o p q r s t ....x
.
.
100 u v w x y z aa bb cc....x
Desired DataFrame
  0 1 2 3 4 5 6 7 8 9....12000
0 a b c 50 d e f  50 g h i j ....x
1 k l m 50 n o p  50 q r s t ....x
.
.
100 u v w 50 x y z 50 aa bb cc....x
                Create new DataFrame by indexing each 3rd column, add .5 for correct sorting and add to original with concat:
df.columns = np.arange(len(df.columns))
df1 = pd.DataFrame(50, index=df.index, columns= df.columns[2::3] + .5)
df2 = pd.concat([df, df1], axis=1).sort_index(axis=1)
df2.columns = np.arange(len(df2.columns))
print (df2)
  0  1  2   3  4  5  6   7  8  9  10  11 12
0  a  b  c  50  d  e  f  50  g  h  i  50  j
1  k  l  m  50  n  o  p  50  q  r  s  50  t
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With