1

I would like to expand and replicate rows and columns multiple times (30 times) in a pandas DataFrame, which has 36 columns and 28 rows. Thus the result should be a 1.080 x 840 matrix.

Example:

    ARG     BRA     BRN
AUS 2.93    3.06    3.56
AUT 5.36    5.47    2.14
BEL 5.36    5.47    2.14

Result should be:

    ARG    ARG   BRA   BRA    BRN   BRN
AUS 2.93   2.93  3.06  3.06   3.56  3.56
AUS 2.93   2.93  3.06  3.06   3.56  3.56
AUT 5.36   5.36  5.47  5.47   2.14  2.14
AUT 5.36   5.36  5.47  5.47   2.14  2.14
BEL 5.36   5.36  5.47  5.47   2.14  2.14   
BEL 5.36   5.36  5.47  5.47   2.14  2.14

Is there a simple way to do so? Thank you in advance!

Alexander Hempfing
  • 247
  • 1
  • 2
  • 9

1 Answers1

2

It is possible by Index.repeat and DataFrame.loc:

N = 2
df = df.loc[df.index.repeat(N), df.columns.repeat(N)]
print (df)
     ARG   ARG   BRA   BRA   BRN   BRN
AUS  2.93  2.93  3.06  3.06  3.56  3.56
AUS  2.93  2.93  3.06  3.06  3.56  3.56
AUT  5.36  5.36  5.47  5.47  2.14  2.14
AUT  5.36  5.36  5.47  5.47  2.14  2.14
BEL  5.36  5.36  5.47  5.47  2.14  2.14
BEL  5.36  5.36  5.47  5.47  2.14  2.14

But then get duplicated columns names, so seelcting is problematic by names:

print (df['ARG'])
      ARG   ARG
AUS  2.93  2.93
AUS  2.93  2.93
AUT  5.36  5.36
AUT  5.36  5.36
BEL  5.36  5.36

For prevent it is possible deduplicate columns names:

df.columns = [f'{a}_{b+1}' for a, b in zip(df.columns, np.arange(df.shape[1]) % N)]
print (df)
     ARG_1  ARG_2  BRA_1  BRA_2  BRN_1  BRN_2
AUS   2.93   2.93   3.06   3.06   3.56   3.56
AUS   2.93   2.93   3.06   3.06   3.56   3.56
AUT   5.36   5.36   5.47   5.47   2.14   2.14
AUT   5.36   5.36   5.47   5.47   2.14   2.14
BEL   5.36   5.36   5.47   5.47   2.14   2.14
BEL   5.36   5.36   5.47   5.47   2.14   2.14
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252