Home > Blockchain >  Update multiple columns after logical subset DataFrames.jl
Update multiple columns after logical subset DataFrames.jl

Time:12-29

I'm looking to update the values of multiple columns in a Julia DataFrame after a logical subset.

In base R, for instance, you could do it like this:

# Mock data
myiris <- iris
myiris$Species <- as.character(myiris$Species)

# Update the values of two columns meeting the criteria
myiris[myiris$Species == "setosa" &
       myiris$Sepal.Length == 5.1,
       c("Species", "Sepal.Width")] <- list("pretty flower", -97)

Both the Species and Sepal.Width columns were provided new values ("pretty flower" and -97, respectively) where the species equals "setosa" and the sepal length equals 5.1.

How do you perform an equivalent operation in Julia? Is there a preferred syntax/function/macro?

CodePudding user response:

Assuming you have data frame such as:

df = MLDatasets.Iris().dataframe;

Than one option you could broadcast row values over selection:

df[ (df.class .== "Iris-setosa") .&& 
      (df.sepallength .== 5.1), [:class, :sepalwidth]] .=  
          DataFrame(, sepalwidth=-97.0)
  • Related