1

I have the a pandas columns containing multiple strings. I want all these strings to be duplicated 3 times.

df = pd.DataFrame(data = ['a','b','c']),

Desired output:

   0
0  a
1  b
2  c

I want to transform this table so it looks like this:

   0
0  a
1  a
2  a
3  b
4  b
5  b
6  c
7  c
8  c

I can't seem to find an easy way to do this.

Anything will help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Herwini
  • 371
  • 1
  • 19
  • Does this answer your question? [How to repeat Pandas data frame?](https://stackoverflow.com/a/23887956/13552470) – Red Jul 25 '20 at 19:15

2 Answers2

4

Try:

df[0].repeat(3).reset_index(drop=True)

Out:

0    a
1    a
2    a
3    b
4    b
5    b
6    c
7    c
8    c
Name: 0, dtype: object
Mark Moretto
  • 2,344
  • 2
  • 15
  • 21
2

You can use repeat + reindex:

df = df.reindex(df.index.repeat(3))
Out[105]: 
   0
0  a
0  a
0  a
1  b
1  b
1  b
2  c
2  c
2  c

Or concat:

df = pd.concat([df]*3)
Red
  • 26,798
  • 7
  • 36
  • 58
BENY
  • 317,841
  • 20
  • 164
  • 234