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.
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).
You can fillna with a series
You can make your df np.nan with mask
This works
df.mask(criteria).fillna(ser)

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