3

The title is more complicated than I was expecting, but I am basically looking for a quick way to make a multiplication table that starts at abitraty integers for both X and Y axises.

My output would be similar to this for X being a range of (5, 12, 1) and Y being a range of (20, 25, 1)

       5      6      7      8      9      10     11
20    100    120    140    160    180    200    220
21    105    126    147    168    189    210    231
22    110    132    154    176    198    220    242
23    115    138    161    184    207    230    253
24    120    144    168    192    216    240    264

I've found this answer which looks similar, but is for indexes. It doesn't seem to do the multiplication I'm looking for.

Community
  • 1
  • 1

1 Answers1

6

NumPy broadcasting for pandas!

row = np.arange(20,25)
col = np.arange(5,12)
df = pd.DataFrame(row[:,None]*col,index=row,columns=col)

Sample run -

In [224]: row = np.arange(20,25)

In [225]: col = np.arange(5,12)

In [226]: pd.DataFrame(row[:,None]*col,index=row,columns=col)
Out[226]: 
     5    6    7    8    9    10   11
20  100  120  140  160  180  200  220
21  105  126  147  168  189  210  231
22  110  132  154  176  198  220  242
23  115  138  161  184  207  230  253
24  120  144  168  192  216  240  264
Divakar
  • 218,885
  • 19
  • 262
  • 358