Home > OS >  Replace multiple strings with multiple values in Julia
Replace multiple strings with multiple values in Julia

Time:10-16

In Python pandas you can pass a dictionary to df.replace in order to replace every matching key with its corresponding value. I use this feature a lot to replace word abbreviations in Spanish that mess up sentence tokenizers.

Is there something similar in Julia? Or even better, so that I (and future users) may learn from the experience, any ideas in how to implement such a function in Julia's beautiful and performant syntax?

Thank you!

Edit: Adding an example as requested

Input:

julia> DataFrames.DataFrame(Dict("A" => ["This is an ex.", "This is a samp.", "This is a samp. of an ex."]))
3×1 DataFrame
 Row │ A                  
     │ String             
─────┼────────────────────
   1 │ This is an ex.
   2 │ This is a samp.
   3 │ This is a samp. of an ex.

Desired output:

3×1 DataFrame
 Row │ A                  
     │ String             
─────┼────────────────────
   1 │ This is an example
   2 │ This is a sample
   3 │ This is a sample of an example

CodePudding user response:

In Julia the function for this is also replace. It takes a collection and replaces elements in it. The simplest form is:

julia> x = ["a", "ab", "ac", "b", "bc", "bd"]
6-element Vector{String}:
 "a"
 "ab"
 "ac"
 "b"
 "bc"
 "bd"

julia> replace(x, "a" => "aa", "b" => "bb")
6-element Vector{String}:
 "aa"
 "ab"
 "ac"
 "bb"
 "bc"
 "bd"

If you have more complex replace pattern you can pass a function that does the replacement:

julia> replace(x) do s
           length(s) == 1 ? s^2 : s
       end
6-element Vector{String}:
 "aa"
 "ab"
 "ac"
 "bb"
 "bc"
 "bd"

There is also replace! that does the same in-place.

Is this what you wanted?

  • Related