# Remove spaces and units in Dataframe header?

**URL:** <https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [September 4, 2020, 1:44pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061 "2020-09-04T13:44:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 4, 2020, 1:44pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/1 "2020-09-04T13:44:29Z")

</div>

Hello!

I am using DataFrames on a CSV file with space and unit declarations next to the variable, for an example:

```julia
time [s]

```

So when I import it, it looks like:

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

How would I go about removing " [s]", so it would become “time”?

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 4, 2020, 1:53pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/2 "2020-09-04T13:53:15Z")

</div>

This is my hardcoded approach

```julia
df = DataFrame(CSV.File("GaugesSWL_Swl_WaveLength.csv",delim=";"))
rename!(df, Symbol.(replace.(string.(names(df)), Ref(r"\[m\]"=>""))))
rename!(df, Symbol.(replace.(string.(names(df)), Ref(r"\[s\]"=>""))))
rename!(df, Symbol.(replace.(string.(names(df)), Ref(r"\s"=>""))))

```

---

<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 4, 2020, 1:55pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/3 "2020-09-04T13:55:27Z")

</div>

Yeah that seems about right. You could always tweak the regex to capture anything of the pattern `[*]`

You shouldn’t need to use `Symbol` at all. In the up-to-date version of DataFrames it should just work with strings.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 4, 2020, 1:56pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/4 "2020-09-04T13:56:35Z")

</div>

`DataConvenience.cleannames!` from DataConvenience.jl

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [September 4, 2020, 2:01pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/5 "2020-09-04T14:01:52Z")

</div>

I am not very good at regex, tried to do what you suggested but it didn’t work for me. You are right about strings though, that was quite functional

Kind regards

@xiaodai I will keep this in mind if I need to do this a lot more

---

<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 4, 2020, 2:06pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/6 "2020-09-04T14:06:13Z")

</div>

You could also do the following

```julia
julia> x = "a string [m]";

julia> t = findfirst('[', x);

julia> x2 = x[1:(t-1)] |> strip
"a string"

```

---

<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:** [September 4, 2020, 2:11pm UTC](https://discourse.julialang.org/t/remove-spaces-and-units-in-dataframe-header/46061/7 "2020-09-04T14:11:45Z")

</div>

just remember that this works when you know that you have a space before `[` (which is ASCII so you can safely subtract `1` otherwise `prevind` has to be used)
