ash90
ash90

Reputation: 123

Adding a column to a dataframe after every nth column

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

Upvotes: 4

Views: 2321

Answers (3)

BENY
BENY

Reputation: 323326

So here is one solution

s=pd.concat([y.assign(new=50) for x, y in df.groupby(np.arange(df.shape[1])//3,axis=1)],axis=1)
s.columns=np.arange(s.shape[1])

Upvotes: 2

piRSquared
piRSquared

Reputation: 294488

Numpy

# How many columns to group
x    = 3
# Get the shape of things
a    = df.to_numpy()
m, n = a.shape
k    = n // x
# Get only a multiple of x columns and reshape
b    = a[:, :k * x].reshape(m, k, x)
# Get the other columns missed by b
c    = a[:, k * x:]
# array of 50's that we'll append to the last dimension
_50  = np.ones((m, k, 1), np.int64) * 50
# append 50's and reshape back to 2D
d    = np.append(b, _50, axis=2).reshape(m, k * (x + 1))
# Create DataFrame while appending the missing bit
pd.DataFrame(np.append(d, c, axis=1))

   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

Setup

df = pd.DataFrame(np.reshape([*'abcdefghijklmnopqrst'], (2, -1)))

Upvotes: 2

jezrael
jezrael

Reputation: 863166

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

Upvotes: 6

Related Questions