I would like to replace some of the values in the foll. dataframe:
dataframe_a
Y2000 Y2001 Y2002 Y2003 Y2004 Item Item Code
34 43 0 0 25 Test Val
I would like to replace the values in the columns with a numeric value derived by multiplying a scalar (say 0.5) by all values in this dataframe:
dataframe_b
Y2000 Y2001 Y2002 Y2003 Y2004 Item Item Code
34 43 10 20 25 Test Val
So, in dataframe_a value for column Y2002 should be 10 * 0.5 and value for column Y2003 should be 20 * 0.5
Currently, I am doing this:
df = dataframe_a[dataframe_a == 0]
df = df * dataframe_b * 0.5
However, not sure how I can update dataframe_a with the new values
You can use the boolean mask and then call fillna:
In [58]:
fill = df2.select_dtypes(include = [np.number]) * 0.5
df1 = df1[df1!=0].fillna(fill)
df1
Out[58]:
Y2000 Y2001 Y2002 Y2003 Y2004 Item Item Code
0 34 43 5 10 25 Test Val
Here df1[df1 !=0] will produce a df of the same shape with NaN values where the condition is not met, you can then call fillna on this and pass the other df which will replace the NaN values where the index and columns align.
The result of the boolean mask:
In [63]:
df1[df1!=0]
Out[63]:
Y2000 Y2001 Y2002 Y2003 Y2004 Item Item Code
0 34 43 NaN NaN 25 Test Val
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