0

I have been trying for hours to find a method to replicate columns n number of times and add them a Dataframe but has had little luck. Please help!

Current Dataframe:

    0
0   2
1   4
2   5
3   6
4   9

Output:

    0  1 2 ... 99
0   2  2 2     2
1   4  4 4     4 
2   5  5 5     5
3   6  6 6     6
4   9  9 9     9
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
Huy Pham
  • 71
  • 6
  • ``pd.concat([df]*100, axis = 'columns')`` possibly? for column names, you could pass ``df.columns = np.arange(0, len(df)) ``after concatenation – sammywemmy Mar 14 '21 at 04:46

3 Answers3

1

As mention in comment by @sammywemmy you can use:-

df=pd.concat([df]*100, axis = 'columns')

After that rename columns:-

df.columns=range(0,len(df.columns))
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
1
>>> df
   0
0  2
1  4
2  5
3  6
4  9

.iloc is another option

>>> df.iloc[:, [0] * 10]
   0  0  0  0  0  0  0  0  0  0
0  2  2  2  2  2  2  2  2  2  2
1  4  4  4  4  4  4  4  4  4  4
2  5  5  5  5  5  5  5  5  5  5
3  6  6  6  6  6  6  6  6  6  6
4  9  9  9  9  9  9  9  9  9  9
0

There could be several ways, one way is something like this:

df= pd.DataFrame()
    for i in range(5):
        for j in range(0,10):
            df.loc[i,j] = i+j
        
df
     0    1    2    3    4    5     6     7     8     9
0  0.0  1.0  2.0  3.0  4.0  5.0   6.0   7.0   8.0   9.0
1  1.0  2.0  3.0  4.0  5.0  6.0   7.0   8.0   9.0  10.0
2  2.0  3.0  4.0  5.0  6.0  7.0   8.0   9.0  10.0  11.0
3  3.0  4.0  5.0  6.0  7.0  8.0   9.0  10.0  11.0  12.0
4  4.0  5.0  6.0  7.0  8.0  9.0  10.0  11.0  12.0  13.0
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45