Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be accomplished quite simply with the DataFrame method <code>apply</code>.</p> <pre><code>In[1]: import pandas as pd; import numpy as np In[2]: df = pd.DataFrame(np.arange(40.).reshape((8, 5)), columns=list('abcde')); df Out[2]: a b c d e 0 0 1 2 3 4 1 5 6 7 8 9 2 10 11 12 13 14 3 15 16 17 18 19 4 20 21 22 23 24 5 25 26 27 28 29 6 30 31 32 33 34 7 35 36 37 38 39 In[3]: ser = pd.Series(np.arange(8) * 10); ser Out[3]: 0 0 1 10 2 20 3 30 4 40 5 50 6 60 7 70 </code></pre> <p>Now that we have our <code>DataFrame</code> and <code>Series</code> we need a function to pass to <code>apply</code>.</p> <pre><code>In[4]: func = lambda x: np.asarray(x) * np.asarray(ser) </code></pre> <p>We can pass this to <code>df.apply</code> and we are good to go </p> <pre><code>In[5]: df.apply(func) Out[5]: a b c d e 0 0 0 0 0 0 1 50 60 70 80 90 2 200 220 240 260 280 3 450 480 510 540 570 4 800 840 880 920 960 5 1250 1300 1350 1400 1450 6 1800 1860 1920 1980 2040 7 2450 2520 2590 2660 2730 </code></pre> <p><code>df.apply</code> acts column-wise by default, but it can can also act row-wise by passing <code>axis=1</code> as an argument to <code>apply</code>.</p> <pre><code>In[6]: ser2 = pd.Series(np.arange(5) *5); ser2 Out[6]: 0 0 1 5 2 10 3 15 4 20 In[7]: func2 = lambda x: np.asarray(x) * np.asarray(ser2) In[8]: df.apply(func2, axis=1) Out[8]: a b c d e 0 0 5 20 45 80 1 0 30 70 120 180 2 0 55 120 195 280 3 0 80 170 270 380 4 0 105 220 345 480 5 0 130 270 420 580 6 0 155 320 495 680 7 0 180 370 570 780 </code></pre> <p>This could be done more concisely by defining the anonymous function inside <code>apply</code></p> <pre><code>In[9]: df.apply(lambda x: np.asarray(x) * np.asarray(ser)) Out[9]: a b c d e 0 0 0 0 0 0 1 50 60 70 80 90 2 200 220 240 260 280 3 450 480 510 540 570 4 800 840 880 920 960 5 1250 1300 1350 1400 1450 6 1800 1860 1920 1980 2040 7 2450 2520 2590 2660 2730 In[10]: df.apply(lambda x: np.asarray(x) * np.asarray(ser2), axis=1) Out[10]: a b c d e 0 0 5 20 45 80 1 0 30 70 120 180 2 0 55 120 195 280 3 0 80 170 270 380 4 0 105 220 345 480 5 0 130 270 420 580 6 0 155 320 495 680 7 0 180 370 570 780 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload