# Rename column if its name matches to a name in given list

**URL:** <https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386>\
**Category:** Data\
**Tags:** dataframes, csv\
**Created:** [August 2, 2023, 9:04am UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386 "2023-08-02T09:04:43Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 5, 2023, 4:59pm UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/21 "2023-08-05T16:59:30Z")

</div>

> [@raman\_kumar](#):
>
> It is still giving error. I have invoked `rename!` function inside `if` conditional loop.

OK. You’ll need to check the actual error you get (instead of only printing that there is a generic error) - and try to debug it.

At this point, it seems that you only know that there is an error.

The `catch` block should not be used in a way that will hide what is actually going on from you.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [August 5, 2023, 5:24pm UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/22 "2023-08-05T17:24:28Z")

</div>

> [@algunion](#):
>
> The `catch` block should not be used in a way that will hide what is actually going on from you.

On adding `rethrow()` in code it says that `df` is not defined. How can i define `df` above if function `change()` is outside `for` loop.

```julia
catch e
println("error ") 
rethrow(e)

```

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

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 5, 2023, 5:30pm UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/23 "2023-08-05T17:30:24Z")

</div>

> [@raman\_kumar](#):
>
> On adding `rethrow()` in code it says that `df` is not defined.

Indeed. Now, please inspect your `change` function. You can see that you are using the `df`, which you didn’t pass to the function as an argument (and it is also not accessible from the function scope).

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [August 5, 2023, 5:39pm UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/24 "2023-08-05T17:39:36Z")

</div>

> [@algunion](#):
>
> You can see that you are using the `df`, which you didn’t pass to the function as an argument

@algunion @kellertuer Good night 🥰  
see changes.I will respond tomorrow .

> **Summary**
>
> ```julia
> using HTTP, CSV, DataFrames
> function doanalysis()
> dfg=nothing
> function change(df)
> name=names(df)
> list = ["time", "Tmid", "T0", "t-T0", "t-to"] # rename to "Time"
> listb = ["mag", "Limit", "Magnitude", "OT", "magn"]
> for i in 1:length(name)
> if name[i] in list ; rename!(df,Dict("name[i]"=>"Time")); end
> if name[i] in listb ; rename!(df,Dict("name[i]"=>"Mag")); end
> i=+1
> end
> end
> for x in 31520
> print("\r peeking at GCN $x ")
> try
> url = "https://gcn.nasa.gov/circulars/$x.txt"
> resp = HTTP.get(url) 
> status=resp.status
> print(" ",status," "); 
> if status == 404 ; println("status=",status); continue; end          
> txt = String(resp.body)
> grb_rexp=r"GRB ?\d{6}([A-G]|(\.\d{2}))?"
> m=match(grb_rexp,txt)
> grb="nogrb"
> if occursin(grb_rexp,txt)
> print(m.match)
> grb=m.match
> end
> if occursin("IKI", txt)
> println(" IKI report")                
> he=first(findfirst(r"^(Date)|(UT start)|(T0+)"m,txt))
> lr=first(findnext(r"^\nThe |(Photometry)"m,txt,he))-1
> cltxt=replace(txt[he:lr], ","=>" ",r"(�+)"=>" " , "+/-"=>" ",">"=>" ","(mid)"=>" ",r"UT ?start"i=>"UT_start")
> df=CSV.read(IOBuffer(cltxt), DataFrame, skipto=3, delim=" ", ignorerepeated=true)
> df.GCN=[x for i in 1:nrow(df)]
> df.GRB=[m.match for i in 1:nrow(df)] 
> change(df)
> if isnothing(dfg) 
> dfg=df
> else
> @show dfg=vcat(dfg,df,cols=:union)
> end # if x is first
> end # if occursin
> catch e
> rethrow(e)
> end # trycatch
> end # for loop
> end
> doanalysis()
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 5, 2023, 7:18pm UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/25 "2023-08-05T19:18:53Z")

</div>

@raman_kumar,

From our interaction so far (e.g., code snippets you presented and the type of questions), I suggest you go over a Julia tutorial/manual to get a basic handle on how the language works (function definition, scope, debugging, etc.).

Also, it is essential you follow up with the error messages (which, in this scenario, are pretty informative) and change your code accordingly.

You can always come here and ask for help - and I am sure you’ll find lots of people willing to help, myself included, but I think it is important that your questions are related to either the language itself or some packages issues that are not properly covered by the package documentation.

My impression (both derived from this thread interaction but also from other topics we interacted on) is that your posts are either requesting some code delivery or step-by-step instructions on what you need to do to achieve some functionality you demand.

I think just _consuming_ code/instructions is not an actual learning experience, and you need to return for more of the same thing. Again and again.

Please read this through the right lens: I am trying to help here. So don’t feel embarrassed - we are all on a learning path (regardless of our level of experience). I am learning daily from how others answer challenging questions and reading great code others wrote.

Also - don’t get me wrong: I do not want to discourage you from a hands-on approach - there is obvious value in that (and the best way to learn).

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [August 17, 2023, 9:31am UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/26 "2023-08-17T09:31:59Z")

</div>

I am still stuck @algunion

> **see code**
>
> ```julia
> using HTTP, CSV, DataFrames
> function doanalysis()
> dfg=nothing
> function change(df)
> @show name=names(df)
> @show name[3]
> list = ["time", "Tmid", "T0", "t-T0", "t-to"] # rename to "Time"
> listb = ["mag", "Limit", "Magnitude", "OT", "magn"]
> for i in 1:length(name)
> if @show name[i] in list
> rename!(df,Dict("name[i]"=>"Time"))
> elseif name[i] in listb ; rename!(df,Dict("name[i]"=>"Mag"))
> else 
> continue
> end
> i=+1
> end
> end
> for x in 31520
> print("\r peeking at GCN $x ")
> try
> url = "https://gcn.nasa.gov/circulars/$x.txt"
> resp = HTTP.get(url) 
> status=resp.status
> print(" ",status," "); 
> if status == 404 ; println("status=",status); continue; end          
> txt = String(resp.body)
> grb_rexp=r"GRB ?\d{6}([A-G]|(\.\d{2}))?"
> m=match(grb_rexp,txt)
> grb="nogrb"
> if occursin(grb_rexp,txt)
> print(m.match)
> grb=m.match
> end
> if occursin("IKI", txt)
> println(" IKI report")                
> he=first(findfirst(r"^(Date)|(UT start)|(T0+)"m,txt))
> lr=first(findnext(r"^\nThe |(Photometry)"m,txt,he))-1
> cltxt=replace(txt[he:lr], ","=>" ",r"(�+)"=>" " , "+/-"=>" ",">"=>" ","(mid)"=>" ",r"UT ?start"i=>"UT_start")
> df=CSV.read(IOBuffer(cltxt), DataFrame, skipto=3, delim=" ", ignorerepeated=true)
> df.GCN=[x for i in 1:nrow(df)]
> df.GRB=[m.match for i in 1:nrow(df)] 
> change(df)
> if isnothing(dfg) 
> dfg=df
> else
> @show dfg=vcat(dfg,df,cols=:union)
> end # if x is first
> end # if occursin
> catch e
> rethrow(e)
> end # trycatch
> end # for loop
> end
> doanalysis()
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 17, 2023, 9:39am UTC](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386/27 "2023-08-17T09:39:51Z")

</div>

Please inspect the error: it is clearly pointing out that you want to rename `:name[i]` to something else - but `:name[i]` doesn’t exists.

If you take a look at your code you can see that you actually use the literal string `"name[i]"` - using `name[i]` will attempt to look up the `name` object and get index `i`. But `"name[i]"` is just a string that reads name[i].

[Previous page](https://discourse.julialang.org/t/rename-column-if-its-name-matches-to-a-name-in-given-list/102386.md?page=1)
