Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 593
Dickster Avatar asked Feb 16 '26 15:02

Dickster


2 Answers

You can try this:

df.loc[criteria, ser.index] = ser[np.newaxis, :]

This ensures proper broadcasting (by np.newaxis) and that column index is honored (by specifying ser.index).

like image 148
YS-L Avatar answered Feb 19 '26 05:02

YS-L


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


like image 40
piRSquared Avatar answered Feb 19 '26 05:02

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!