# Replace Values in a DataFrames Column with Another Value

**URL:** <https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548>\
**Category:** Data\
**Tags:** data, dataframes\
**Created:** [April 15, 2022, 7:26pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548 "2022-04-15T19:26:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 15, 2022, 7:26pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/1 "2022-04-15T19:26:44Z")

</div>

In my dataframe I have several columns which values are string  
I would like to extract those values and then doing it’s preprocessing  
Thnak you for your help

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 15, 2022, 7:35pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/2 "2022-04-15T19:35:37Z")

</div>

Hey @Lamis_Njeh ,

Could you provide an example of what your data frame looks like (don’t need actual values, there - just examples).  
Thanks!

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 15, 2022, 7:41pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/3 "2022-04-15T19:41:16Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/c/3c6d8c45a7e0a65f3a895a81e6bb3b199552917f.png)  
I would like to replace “negative” with 0 and “positive” with 1

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 15, 2022, 7:53pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/4 "2022-04-15T19:53:18Z")

</div>

You could do something like this:

```julia
df["SARS-Cov-2 exam result"] = [item == "negative" ? 0 : 1 for item in df["SARS-Cov-2 exam result"]]

```

Where `df` is your DataFrame.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [April 15, 2022, 8:16pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/5 "2022-04-15T20:16:57Z")

</div>

```julia
df."SARS-Cov-2 exam result" = ifelse.(df."SARS-Cov-2 exam result" .== "negative", 0, 1)

```

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [April 15, 2022, 8:21pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/6 "2022-04-15T20:21:42Z")

</div>

The package includes functions for data manipulation, e.g. `transform`

```julia
map_to_Int(s) = s == "negativ" ? 0 : (s == "positiv" ? 1 : missing)
col = Symbol("SARS-Cov-2 exam result")
transform!(df, col => ByRow(map_to_Int) => col)

```

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 15, 2022, 9:08pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/7 "2022-04-15T21:08:03Z")

</div>

thanks sir it’s very helpful  
sir there is a column of string but it contains a lot of different values and I’m not sure about doing that to all values ( an other column)  
how could I be sure about all values in a column ?  
thanks sir for your help

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 15, 2022, 9:29pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/8 "2022-04-15T21:29:24Z")

</div>

No problem!  
Could you provide more information on these values?

My solution would be something like this based on what you are saying:

```julia
value_map = Dict("positive" => 1, "negative" => 0, "another_string" => 42)
df["SARS-Cov-2 exam result"] = [value_map[item] for item in df["SARS-Cov-2 exam result"]]

```

This is the most simple, straightforward approach that comes to my mind.  
You would have to define the mappings in the dictionary.  
If you provide an example of the other column, we could perhaps help better.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [April 15, 2022, 9:44pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/9 "2022-04-15T21:44:56Z")

</div>

I’m not sure if I understand your question. It’s possible though to pass several arguments to the function that is applied to each row. E.g. the result could depend on some condition to column `c2`

```julia
function map_to_Int(c1, c2)
    if c2 == some_condition
        return c1 == "negative" ? 0 : (c1 == "positive" ? 1 : missing)
    else
        return 9
    end
end

transform!(df, [:c1, :c2] => ByRow(map_to_Int) => :r)

```

---

<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:** [April 15, 2022, 10:12pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/10 "2022-04-15T22:12:23Z")

</div>

using DataFramesMeta.jl

```julia
@rtransform df @passmissing $"SARS-Cov-2 exam result" == "positive" ? true : false

```

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 15, 2022, 10:17pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/11 "2022-04-15T22:17:48Z")

</div>

![image.png](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d362160ad1a0c70efa238313aefdd0cc21a73ff.png)

this is my goal but with python

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 16, 2022, 12:21am UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/12 "2022-04-16T00:21:53Z")

</div>

This seems like a different goal than what you had stated at the start of the post which is fine.  
Initially, it appeared you wanted to map the string values found in one columns, “SARS-Cov-2 exam result”, to 1’s and 0’s.  
I think the previous answers provided work well for what you were trying to do initially.

If you want to find every single unique item in all columns of a dataframe, you could do something like this:

```julia
> df = DataFrame(rand(["Kyrylo", "Layla", "Bob", "Noelle", "Deepak"], 3, 5), :auto)
> 3×5 DataFrame
 Row │ x1 x2 x3 x4 x5
     │ String String String String String
─────┼────────────────────────────────────────
   1 │ Noelle Layla Kyrylo Bob Kyrylo
   2 │ Noelle Noelle Deepak Bob Bob
   3 │ Noelle Kyrylo Deepak Bob Layla

> for column in propertynames(df)
      println("Column $column: $(df[:, column])")
  end

Column x1: ["Noelle", "Noelle", "Noelle"]
Column x2: ["Layla", "Noelle", "Kyrylo"]
Column x3: ["Kyrylo", "Deepak", "Deepak"]
Column x4: ["Bob", "Bob", "Bob"]
Column x5: ["Kyrylo", "Bob", "Layla"]

```

So, using my previous answer, you could then replace values as needed:

```julia
>value_map = Dict("Kyrylo" => 1, "Layla" => 0, "Bob" => 3, "Noelle" => 5, "Deepak" => 17)
>for column in propertynames(df)
        df[!, column] = [value_map[item] for item in df[:, column]]
end
> df
> 3×5 DataFrame
 Row │ x1 x2 x3 x4 x5
     │ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
   1 │ 5 0 1 3 1
   2 │ 5 5 17 3 3
   3 │ 5 1 17 3 0

```

So in summary, if I can make sure I know what you are asking, you want to know:

1. How to find all unique values in a column of a dataframe
2. Map values in a dataframe to another value
3. Replace values in a dataframe given a mapping

Is that right?  
I think this should answer all the questions you have had.  
Let us know if that helped!

---

<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:** [April 16, 2022, 1:40pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/13 "2022-04-16T13:40:41Z")

</div>

I try a different interpretation of the OP’s request.  
He would like to apply the solution Negative =\> 0, Positive =\> 1, to other columns (BUT NOT ALL) for “dected” non-detected "strings.  
Perhaps he fears that for the columns that have some values in common, changing one column can propagate it on the others, without the possibility of control.

PS  
Is it possible (and how?) In julia a syntax of the print () function that produces a result like the one shown in the figure?

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 17, 2022, 6:48pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/14 "2022-04-17T18:48:03Z")

</div>

I would like first of all to detect all string values in my dataframe  
then I whould like to replace it

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 18, 2022, 8:28pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/15 "2022-04-18T20:28:43Z")

</div>

thank you it’s very helpfull

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 18, 2022, 8:42pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/16 "2022-04-18T20:42:46Z")

</div>

it’s right but I’m sorry that I have some troubles with english

---

<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:** [April 19, 2022, 8:46am UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/17 "2022-04-19T08:46:17Z")

</div>

i tried to simulate python output in julia.  
I found a db with similar data to yours in [this site](https://www.kaggle.com/datasets/imdevskp/corona-virus-report/download)

and I produced the following output …

```julia
julia> H
29×2 Matrix{String}:
 "SARS-Cov-2 exam result----------------------------" "[negative, positive]"
 "Respiratory Syncytial Virus-----------------------" "[missing, not_detected, detected]"
 "Influenza A---------------------------------------" "[missing, not_detected, detected]"
 "Influenza B---------------------------------------" "[missing, not_detected, detected]"
 "Parainfluenza 1-----------------------------------" "[missing, not_detected, detected]"
 "CoronavirusNL63-----------------------------------" "[missing, not_detected, detected]"
 "Rhinovirus/Enterovirus----------------------------" "[missing, detected, not_detected]"
 "Coronavirus HKU1----------------------------------" "[missing, not_detected, detected]"
 "Parainfluenza 3-----------------------------------" "[missing, not_detected, detected]"
 "Chlamydophila pneumoniae--------------------------" "[missing, not_detected, detected]"
 "Adenovirus----------------------------------------" "[missing, not_detected, detected]"
 "Parainfluenza 4-----------------------------------" "[missing, not_detected, detected]"
 "Coronavirus229E-----------------------------------" "[missing, not_detected, detected]"
 "CoronavirusOC43-----------------------------------" "[missing, not_detected, detected]"
 "Inf A H1N1 2009-----------------------------------" "[missing, not_detected, detected]"
 "Bordetella pertussis------------------------------" "[missing, not_detected, detected]"
 "Metapneumovirus-----------------------------------" "[missing, not_detected, detected]"
 "Parainfluenza 2-----------------------------------" "[missing, not_detected]"
 "Influenza B, rapid test---------------------------" "[missing, negative, positive]"
 "Influenza A, rapid test---------------------------" "[missing, negative, positive]"
 "Strepto A-----------------------------------------" "[missing, positive, negative, not_done]"
 "Urine - Esterase----------------------------------" "[missing, absent, not_done]"
 "Urine - Hemoglobin--------------------------------" "[missing, absent, present, not_done]"
 "Urine - Bile pigments-----------------------------" "[missing, absent, not_done]"
 "Urine - Ketone Bodies-----------------------------" "[missing, absent, not_done]"
 "Urine - Nitrite-----------------------------------" "[missing, not_done]"
 "Urine - Urobilinogen------------------------------" "[missing, normal, not_done]"
 "Urine - Protein-----------------------------------" "[missing, absent, not_done]"
 "Urine - Color-------------------------------------" "[missing, light_yellow, yellow, orange, citrus_yellow]"

```

```julia
using DataFrames, CSV

df=CSV.read("dataset.csv",decimal=',', DataFrame)

describe(df)

df1=df[:,Not(["Patient ID","Urine - pH"])]

colstr=names(df1, eltype.(eachcol(df1)) .>: String15)

un=unique.(eachcol(df[:,colstr]))

re=r"\[([^]]+)\]"

m= @. replace(getproperty(match(re,string(un)),:match), "\""=>"")

H= hcat(rpad.(colstr,50,"-"),m)

```

below is the list of unique values present in the “strings” columns.  
How do you want to replace these values with numbers?

```julia
julia> domain= union(un...)
13-element Vector{Union{Missing, String15}}:
 "negative"
 "positive"
 missing
 "not_detected"
 "detected"
 "not_done"
 "absent"
 "present"
 "normal"
 "light_yellow"
 "yellow"
 "orange"
 "citrus_yellow"

```

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 19, 2022, 2:31pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/18 "2022-04-19T14:31:28Z")

</div>

It’s no problem at all!  
I am glad it helped. 🙂

Also, you may be interested in the package, [PrettyTables.jl](https://ronisbr.github.io/PrettyTables.jl/stable/), as well for printing your tables.  
For example, this could be printed:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/3/b35e41824c615e03c194463b0f3c1fbb710194aa.jpeg)

---

<div class="post-metadata">

**Author:** ![Lamis\_Njeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamis_njeh/32/34292_2.png) [@Lamis\_Njeh](https://discourse.julialang.org/u/Lamis_Njeh)\
**Post date:** [April 19, 2022, 6:36pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/19 "2022-04-19T18:36:39Z")

</div>

Thank you sir it’s very helpful

---

<div class="post-metadata">

**Author:** ![nikit\_goku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikit_goku/32/49765_2.png) [@nikit\_goku](https://discourse.julialang.org/u/nikit_goku)\
**Post date:** [May 23, 2023, 9:21am UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframes-column-with-another-value/79548/20 "2023-05-23T09:21:21Z")

</div>

You could do something like this:

```julia
df["SARS-Cov-2 exam result"] = Int64.(replace(df["SARS-Cov-2 exam result"], "negative"=>0, "positive"=>1))

```
