# Replace all values greater than 1 in a DataFrame with 1

**URL:** <https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 29, 2023, 1:44pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397 "2023-09-29T13:44:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aadam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aadam/32/7113_2.png) [@aadam](https://discourse.julialang.org/u/aadam)\
**Post date:** [September 29, 2023, 1:44pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/1 "2023-09-29T13:44:10Z")

</div>

What’s the most idiomatic way of achieving this in Julia? Can this be done as a one-liner?

Something like this:

```julia
df[df.> 0] .= 1

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 29, 2023, 1:56pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/2 "2023-09-29T13:56:02Z")

</div>

I would maybe do by column.

```julia
julia> for col in eachcol(df)
           col[col .> 0] .= 1
       end

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 29, 2023, 2:02pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/3 "2023-09-29T14:02:14Z")

</div>

You will get bitten in these approaches if you do not filter out non-numeric columns. Use the loop but add an element type check before trying to replace.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 29, 2023, 2:12pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/4 "2023-09-29T14:12:38Z")

</div>

One solution is to use `names` to select real-valued columns.

```julia
julia> for col in eachcol(df[!, names(df, Real)])
           col[col .> .5] .= 100
       end

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 29, 2023, 3:31pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/5 "2023-09-29T15:31:49Z")

</div>

Or if you desperately wanted a one-liner:

```julia
mapcols!(x -> ifelse.(x .> 0, 1.0, x), df)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 30, 2023, 8:58pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/6 "2023-09-30T20:58:43Z")

</div>

```julia

df=DataFrame(rand(10,4).*2, :auto)

DataFrame(min.(Matrix(df),1),names(df))

```

```julia

select(df, names(df,Real).=>x->min.(x,1))

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 30, 2023, 10:14pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/7 "2023-09-30T22:14:43Z")

</div>

Do these have any benefit over existing solutions?

1. The first solution allocates a matrix, which is highly inefficient.
2. The second one does not modify the data frame in place, as OPs question implied.

Do you have any explanation for why you included the solutions you did?

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [September 30, 2023, 10:56pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/8 "2023-09-30T22:56:12Z")

</div>

> [@pdeffebach](#):
>
> Do you have any explanation for why you included the solutions you did?

It’s fine, they’re just suggestions.

Good pointing out how they compare to other solutions though.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [September 30, 2023, 11:10pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/9 "2023-09-30T23:10:55Z")

</div>

Same style as @nilshg 's one-liner, but with column filtering:

```julia
mapcols!(x -> eltype(x)<:Real ? 
  ifelse.(x .> one(eltype(x)), one(eltype(x)), x) : x, df)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 1, 2023, 12:30pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/10 "2023-10-01T12:30:19Z")

</div>

no. Except I had some free time

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 1, 2023, 12:40pm UTC](https://discourse.julialang.org/t/replace-all-values-greater-than-1-in-a-dataframe-with-1/104397/11 "2023-10-01T12:40:10Z")

</div>

this version does not unnecessarily allocate a matrix

```julia
min.(df[:,names(df,Real)],1)

```
