python - Update dataframe values by broadcasting series -


trying update portion of dataframe values series.

np.random.seed(1) df = pd.dataframe(np.random.randint(1,100,(5,5)),columns =list('abcde')) print df         b   c   d   e 0  38  13  73  10  76 1   6  80  65  17   2 2  77  72   7  26  51 3  21  19  85  12  29 4  30  15  51  69  88 

with series:

ser = pd.series(index =list('cbade'),data = range(-5,0))  c   -5 b   -4   -3 d   -2 e   -1 dtype: int64 

lets take slice updating

criteria = df['a'] < 25  criteria: 0    false 1     true 2    false 3     true 4    false 

trying:

df[criteria] = ser df.loc[criteria,:] = ser etc. 

desired output:

       b   c   d   e 0  38  13  73  10   76 1  -3  -4  -5  -2   -1 2  77  72   7  26   51 3  -3  -4  -5  -2   -1 4  30  15  51  69   88 

i want honor column index , ignore row index, using boolean criteria , broadcasting.

you can fillna series
can make df np.nan mask
works

df.mask(criteria).fillna(ser) 

enter image description here



Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -