merchmallow
merchmallow

Reputation: 794

Applying replace over a list of certain columns of a dataframe

I have a list with names of 30 columns in a dataframe.

I have to apply replace to these 30 columns only. I tried:

list = names(df[!,r"Com"]) - ok!

Then I am trying:

replace!(df[!,list],"RR" =>"AA") - Fail

How can I do such transformation(with certain columns at once)?

Upvotes: 1

Views: 432

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

There are several ways to do it. In what I write below I assume you want to update the df data frame in-place (i.e. you do not want a new data frame, but change the existing one).

foreach(col -> replace!(col, "RR" => "AA"), eachcol(df[!, r"Com"]))

or

mapcols!(col -> replace!(col, "RR" => "AA"), df[!, r"Com"])

or

transform!(df, Cols(r"Com") .=> col -> replace!(col, "RR" => "AA"), renamecols=false)

Upvotes: 1

Related Questions