Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia dataframe replace character in column

Tags:

replace

julia

I am working on a dataframe in julia and one column has zipcodes. In certain instances, a State code is appended in front of zip code.

For example: if zipcode is 123456, and state is ab, it looks like ab 123456.

There are multiple values like that in the column. How can I replace all "ab " with "" in that column.

like image 764
Ajit Jain Avatar asked Jun 03 '26 00:06

Ajit Jain


1 Answers

You can use replace function with a regexp matching your pattern and broadcast it over all entries of the column like this:

julia> using DataFrames

julia> df = DataFrame(x = ["ab x", "y", "ab z"])
3×1 DataFrame 
 Row │ x      
     │ String 
─────┼────────
   1 │ ab x   
   2 │ y      
   3 │ ab z   

julia> df.x .= replace.(df.x, r"^ab " => "")
3-element Vector{String}:
 "x"
 "y"
 "z"

julia> df
3×1 DataFrame
 Row │ x      
     │ String 
─────┼────────
   1 │ x
   2 │ y
   3 │ z
like image 129
Bogumił Kamiński Avatar answered Jun 06 '26 05:06

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!