Hello,
I have a dataframe where some rows of a given column (‘group’) have a certain value (‘normal’). I would like to change such a value to something else. What would be the syntax? In other words, I am looking for Julia’s equivalent to R’s
df$group[df32$group == "Normal"] <- "Healthy"
And what would be the syntax for renaming the whole column?
Thank you
There are two easy ways to do what you want.
df = DataFrame(group=["foo", "notfoo", "foo"], data = 1:3)
Using replace
replace!(df.group, "foo" => "bar")
group | data | |
---|---|---|
String | Int64 | |
1 | bar | 1 |
2 | notfoo | 2 |
3 | bar | 3 |
Or by indexing as you do in your example
df[df.group .== "foo","group"] .= "bar"
Both of these will do the change inplace.
1 Like
Thank you. And if I wanted to change the whole column group to “unknown”, for instance?
In that case you can use the bang operator and broadcasting to assign a new value to the whole column
df[!,"group"] .= "unknown"
You can find some more details on the different ways of accessing the data in this blog post
2 Likes