I want to filter a dataframe and append a value to a new/existing column in the dataframe. For example in the following dataframe, I want to append a value of 0.7 to column pre-mean where the month values are equal to 11. So in other words pre_mean column should contain values of 0.7 at rows 2 to 5 while all other columns should have a NaN value.
I tried something like this, but of course it's incorrect.
df[:pre_mean] = ifelse.(df[:month] .== 11, 0.7, df)
In python, you can do this using pd.apply or np.where functions,
#How to do in python
df["pre_mean"] = np.where(df["month"] == 11, 0.7, None)
But I have got no clue how to achieve this in Julia? Any Ideas?
df[df.month .== 11, :pre_mean] .= 0.7
This should work.
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