python 2.7 - Pandas DataFrame: How to get a min value in a vectorized way? -


i have pandas dataframe:

import numpy import pandas  df1         = abs((pandas.dataframe(numpy.random.randn(20, 8))*100).astype(int)) df1.columns = list('abcdefgh')  df1.index   = pandas.date_range('1/1/2014', periods=20) 

enter image description here

how create new column give me minimum value of first half of current row , last 3 values in previous row?

for example, first 5 rows in created column be:

nan  12 4 14 21 

here 1 way it. basically, need first shift last 3 columns , combine first 4 columns, , calculate min.

import numpy import pandas  # data # =================================== numpy.random.seed(0) df1         = abs((pandas.dataframe(numpy.random.randn(20, 8))*100).astype(int)) df1.columns = list('abcdefgh')  df1.index   = pandas.date_range('1/1/2014', periods=20)  # processing # ===================================  df1['custom_min'] = pandas.concat([df1[df1.columns[:5]], df1[df1.columns[-3:]].shift(1)], axis=1).min(axis=1)  print(df1)                   b    c    d    e    f    g    h  custom_min 2014-01-01  176   40   97  224  186   97   95   15          40 2014-01-02   10   41   14  145   76   12   44   33          10 2014-01-03  149   20   31   85  255   65   86   74          12 2014-01-04  226  145    4   18  153  146   15   37           4 2014-01-05   88  198   34   15  123  120   38   30          15 2014-01-06  104  142  170  195   50   43  125   77          30 2014-01-07  161   21   89   38   51  118    2   42          21 2014-01-08    6   30   63   36   67   35   81  172           2 2014-01-09   17   40  163   46   90    5   72   12          17 2014-01-10  113  123   40   68   87   57   31    5           5 2014-01-11  116   90   46  153  148  189  117   17           5 2014-01-12  107  105   40  122   20   97   35   70          17 2014-01-13    1  178   12   40  188  134  127   96           1 2014-01-14  117  194   41   74  192  148  186   90          41 2014-01-15   86  191   26   80   94   15   61   92          26 2014-01-16   37  109   29  132   69   14   43  184          15 2014-01-17   67   40   76   53   67    3   63   67          14 2014-01-18   57   20   39  109  149   43   16   63           3 2014-01-19  238   94   91  111  131   46    6  171          16 2014-01-20   74   82    9   66  112  107  114   43           6 

Comments