Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIltering rows and appending values

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?

enter image description here

like image 798
imantha Avatar asked Dec 17 '22 12:12

imantha


1 Answers

df[df.month .== 11, :pre_mean] .= 0.7

This should work.

like image 91
Andy_101 Avatar answered Dec 29 '22 20:12

Andy_101