Dickster Dickster - 29 days ago 17
Python Question

Update dataframe values by broadcasting series

Trying to update a portion of a dataframe with values from a series.

np.random.seed(1)
df = pd.DataFrame(np.random.randint(1,100,(5,5)),columns =list('ABCDE'))
print df


A 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 a series:

ser = pd.Series(index =list('CBADE'),data = range(-5,0))

C -5
B -4
A -3
D -2
E -1
dtype: int64


Lets take a slice for 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:

A 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 to honor the column index and ignore the row index, using the boolean criteria and broadcasting.

Answer

You can fillna with a series
You can make your df np.nan with mask
This works

df.mask(criteria).fillna(ser)

enter image description here