Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate Pandas dataframe as function of index and columns

I have a dataframe where the index and column are both numbers--i.e.

rng = np.arange(2,51)
box = pd.DataFrame(index = rng, columns = rng)

I want the values of the dataframe to be a function of the index and column--so for instance box[2][2] should equal 4.

Currently I have it in

for x in range(2,len(box)+2):
    for y in range(2,len(box)+2):
        box[x][y] = x*y
box

but I feel like there should be a more elegant solution. Is there a way to do this purely with slicing/dataframe assignment methods?

like image 739
Michael Schweitzer Avatar asked Dec 07 '17 15:12

Michael Schweitzer


3 Answers

Here's what I think you should be doing with numpy broadcasting:

import numpy
import pandas

rng = numpy.arange(2, 51)
box = pandas.DataFrame(index=rng, columns=rng, data=rng*rng[:, None])

In the case that knowing all of the value ahead of time isn't feasible, you could assign them later:

box = pandas.DataFrame(index=rng, columns=rng)
box.iloc[:, :] = some_other_array

And if you really are assigning small subsets at a time, you'd do:

box.iloc[row_positions, column_positions] = values

or

box.loc[row_labels, column_labels] = values
like image 158
Paul H Avatar answered Nov 08 '22 12:11

Paul H


Using apply

box=box.apply(lambda x : x.name*x.index)

Or we recreate the dataframe box

box=pd.DataFrame(box.index.values[:,None]*box.columns.values,index=box.index,columns=box.columns)
like image 1
BENY Avatar answered Nov 08 '22 12:11

BENY


Use np.outer:

box_out = pd.DataFrame(np.outer(box.index, box.columns), index=box.index, columns=box.columns)

Output:

     2    3    4    5    6    7    8    9    10   11  ...     41    42    43    44    45    46    47    48    49    50
2     4    6    8   10   12   14   16   18   20   22  ...     82    84    86    88    90    92    94    96    98   100
3     6    9   12   15   18   21   24   27   30   33  ...    123   126   129   132   135   138   141   144   147   150
4     8   12   16   20   24   28   32   36   40   44  ...    164   168   172   176   180   184   188   192   196   200
5    10   15   20   25   30   35   40   45   50   55  ...    205   210   215   220   225   230   235   240   245   250
6    12   18   24   30   36   42   48   54   60   66  ...    246   252   258   264   270   276   282   288   294   300
7    14   21   28   35   42   49   56   63   70   77  ...    287   294   301   308   315   322   329   336   343   350
8    16   24   32   40   48   56   64   72   80   88  ...    328   336   344   352   360   368   376   384   392   400
9    18   27   36   45   54   63   72   81   90   99  ...    369   378   387   396   405   414   423   432   441   450
10   20   30   40   50   60   70   80   90  100  110  ...    410   420   430   440   450   460   470   480   490   500
11   22   33   44   55   66   77   88   99  110  121  ...    451   462   473   484   495   506   517   528   539   550
12   24   36   48   60   72   84   96  108  120  132  ...    492   504   516   528   540   552   564   576   588   600
13   26   39   52   65   78   91  104  117  130  143  ...    533   546   559   572   585   598   611   624   637   650
14   28   42   56   70   84   98  112  126  140  154  ...    574   588   602   616   630   644   658   672   686   700
15   30   45   60   75   90  105  120  135  150  165  ...    615   630   645   660   675   690   705   720   735   750
16   32   48   64   80   96  112  128  144  160  176  ...    656   672   688   704   720   736   752   768   784   800
17   34   51   68   85  102  119  136  153  170  187  ...    697   714   731   748   765   782   799   816   833   850
18   36   54   72   90  108  126  144  162  180  198  ...    738   756   774   792   810   828   846   864   882   900
19   38   57   76   95  114  133  152  171  190  209  ...    779   798   817   836   855   874   893   912   931   950
20   40   60   80  100  120  140  160  180  200  220  ...    820   840   860   880   900   920   940   960   980  1000
21   42   63   84  105  126  147  168  189  210  231  ...    861   882   903   924   945   966   987  1008  1029  1050
22   44   66   88  110  132  154  176  198  220  242  ...    902   924   946   968   990  1012  1034  1056  1078  1100
23   46   69   92  115  138  161  184  207  230  253  ...    943   966   989  1012  1035  1058  1081  1104  1127  1150
24   48   72   96  120  144  168  192  216  240  264  ...    984  1008  1032  1056  1080  1104  1128  1152  1176  1200
25   50   75  100  125  150  175  200  225  250  275  ...   1025  1050  1075  1100  1125  1150  1175  1200  1225  1250
26   52   78  104  130  156  182  208  234  260  286  ...   1066  1092  1118  1144  1170  1196  1222  1248  1274  1300
27   54   81  108  135  162  189  216  243  270  297  ...   1107  1134  1161  1188  1215  1242  1269  1296  1323  1350
28   56   84  112  140  168  196  224  252  280  308  ...   1148  1176  1204  1232  1260  1288  1316  1344  1372  1400
29   58   87  116  145  174  203  232  261  290  319  ...   1189  1218  1247  1276  1305  1334  1363  1392  1421  1450
30   60   90  120  150  180  210  240  270  300  330  ...   1230  1260  1290  1320  1350  1380  1410  1440  1470  1500
31   62   93  124  155  186  217  248  279  310  341  ...   1271  1302  1333  1364  1395  1426  1457  1488  1519  1550
32   64   96  128  160  192  224  256  288  320  352  ...   1312  1344  1376  1408  1440  1472  1504  1536  1568  1600
33   66   99  132  165  198  231  264  297  330  363  ...   1353  1386  1419  1452  1485  1518  1551  1584  1617  1650
34   68  102  136  170  204  238  272  306  340  374  ...   1394  1428  1462  1496  1530  1564  1598  1632  1666  1700
35   70  105  140  175  210  245  280  315  350  385  ...   1435  1470  1505  1540  1575  1610  1645  1680  1715  1750
36   72  108  144  180  216  252  288  324  360  396  ...   1476  1512  1548  1584  1620  1656  1692  1728  1764  1800
37   74  111  148  185  222  259  296  333  370  407  ...   1517  1554  1591  1628  1665  1702  1739  1776  1813  1850
38   76  114  152  190  228  266  304  342  380  418  ...   1558  1596  1634  1672  1710  1748  1786  1824  1862  1900
39   78  117  156  195  234  273  312  351  390  429  ...   1599  1638  1677  1716  1755  1794  1833  1872  1911  1950
40   80  120  160  200  240  280  320  360  400  440  ...   1640  1680  1720  1760  1800  1840  1880  1920  1960  2000
41   82  123  164  205  246  287  328  369  410  451  ...   1681  1722  1763  1804  1845  1886  1927  1968  2009  2050
42   84  126  168  210  252  294  336  378  420  462  ...   1722  1764  1806  1848  1890  1932  1974  2016  2058  2100
43   86  129  172  215  258  301  344  387  430  473  ...   1763  1806  1849  1892  1935  1978  2021  2064  2107  2150
44   88  132  176  220  264  308  352  396  440  484  ...   1804  1848  1892  1936  1980  2024  2068  2112  2156  2200
45   90  135  180  225  270  315  360  405  450  495  ...   1845  1890  1935  1980  2025  2070  2115  2160  2205  2250
46   92  138  184  230  276  322  368  414  460  506  ...   1886  1932  1978  2024  2070  2116  2162  2208  2254  2300
47   94  141  188  235  282  329  376  423  470  517  ...   1927  1974  2021  2068  2115  2162  2209  2256  2303  2350
48   96  144  192  240  288  336  384  432  480  528  ...   1968  2016  2064  2112  2160  2208  2256  2304  2352  2400
49   98  147  196  245  294  343  392  441  490  539  ...   2009  2058  2107  2156  2205  2254  2303  2352  2401  2450
50  100  150  200  250  300  350  400  450  500  550  ...   2050  2100  2150  2200  2250  2300  2350  2400  2450  2500

If you want other functions look at numpy.ufunc.outer and you can use this list of ufunc. Such:

box_add_outer = pd.DataFrame(np.add.outer(box.index, box.columns), index=box.index, columns=box.columns)

Output:

    2   3   4   5   6   7   8   9   10  11 ...   41  42  43  44  45  46  47  48  49   50
2    4   5   6   7   8   9  10  11  12  13 ...   43  44  45  46  47  48  49  50  51   52
3    5   6   7   8   9  10  11  12  13  14 ...   44  45  46  47  48  49  50  51  52   53
4    6   7   8   9  10  11  12  13  14  15 ...   45  46  47  48  49  50  51  52  53   54
5    7   8   9  10  11  12  13  14  15  16 ...   46  47  48  49  50  51  52  53  54   55
6    8   9  10  11  12  13  14  15  16  17 ...   47  48  49  50  51  52  53  54  55   56
7    9  10  11  12  13  14  15  16  17  18 ...   48  49  50  51  52  53  54  55  56   57
8   10  11  12  13  14  15  16  17  18  19 ...   49  50  51  52  53  54  55  56  57   58
9   11  12  13  14  15  16  17  18  19  20 ...   50  51  52  53  54  55  56  57  58   59
10  12  13  14  15  16  17  18  19  20  21 ...   51  52  53  54  55  56  57  58  59   60
11  13  14  15  16  17  18  19  20  21  22 ...   52  53  54  55  56  57  58  59  60   61
12  14  15  16  17  18  19  20  21  22  23 ...   53  54  55  56  57  58  59  60  61   62
13  15  16  17  18  19  20  21  22  23  24 ...   54  55  56  57  58  59  60  61  62   63
14  16  17  18  19  20  21  22  23  24  25 ...   55  56  57  58  59  60  61  62  63   64
15  17  18  19  20  21  22  23  24  25  26 ...   56  57  58  59  60  61  62  63  64   65
16  18  19  20  21  22  23  24  25  26  27 ...   57  58  59  60  61  62  63  64  65   66
17  19  20  21  22  23  24  25  26  27  28 ...   58  59  60  61  62  63  64  65  66   67
18  20  21  22  23  24  25  26  27  28  29 ...   59  60  61  62  63  64  65  66  67   68
19  21  22  23  24  25  26  27  28  29  30 ...   60  61  62  63  64  65  66  67  68   69
20  22  23  24  25  26  27  28  29  30  31 ...   61  62  63  64  65  66  67  68  69   70
21  23  24  25  26  27  28  29  30  31  32 ...   62  63  64  65  66  67  68  69  70   71
22  24  25  26  27  28  29  30  31  32  33 ...   63  64  65  66  67  68  69  70  71   72
23  25  26  27  28  29  30  31  32  33  34 ...   64  65  66  67  68  69  70  71  72   73
24  26  27  28  29  30  31  32  33  34  35 ...   65  66  67  68  69  70  71  72  73   74
25  27  28  29  30  31  32  33  34  35  36 ...   66  67  68  69  70  71  72  73  74   75
26  28  29  30  31  32  33  34  35  36  37 ...   67  68  69  70  71  72  73  74  75   76
27  29  30  31  32  33  34  35  36  37  38 ...   68  69  70  71  72  73  74  75  76   77
28  30  31  32  33  34  35  36  37  38  39 ...   69  70  71  72  73  74  75  76  77   78
etc....
like image 1
Scott Boston Avatar answered Nov 08 '22 13:11

Scott Boston