python - doing calculations in pandas dataframe based on trailing row -
is possible calculations in pandas dataframe based on trailing rows in different column? this.
frame = pd.dataframe({'a' : [true, false, true, false],                   'b' : [25, 22, 55, 35]})   i want output this:
a     b     c true  25     false 22   44 true  55   55 false 35   70   where column c same column b when trailing row in column false , column c column b * 2 when trailing row in column true?
you use where series method:
in [11]: frame['b'].where(frame['a'], 2 * frame['b']) out[11]: 0    25 1    44 2    55 3    70 name: b, dtype: int64  in [12]: frame['c'] = frame['b'].where(frame['a'], 2 * frame['b'])   alternatively use apply (but slower):
in [21]: frame.apply(lambda x: 2 * x['b'] if x['a'] else x['b'], axis=1   since using "trailing row" going need use shift:
in [31]: frame['a'].shift() out[31]: 0      nan 1     true 2    false 3     true name: a, dtype: object  in [32]: frame['a'].shift().fillna(false)  # not needed, perhaps clearer out[32]: 0    false 1     true 2    false 3     true name: a, dtype: object   and use other way around:
in [33]: c = (2 * frame['b']).where(frame['a'].shift().fillna(false), frame['b'])  in [34]: c out[34]: 0    25 1    44 2    55 3    70 name: b, dtype: int64   and change first row (e.g. nan, in pandas use nan missing data)
in [35]: c = c.astype(np.float)  # needs accept nan  in [36]: c.iloc[0] = np.nan  in [36]: frame['c'] = c  in [37]: frame out[37]:          b   c 0   true  25 nan 1  false  22  44 2   true  55  55 3  false  35  70      
Comments
Post a Comment