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

And it is expected not to work - because the names to be changed need to also be in the column names of your data frame.

If you do intersect(names(dataframe), list), it will work:

names(dataframe)=["Date", "UT_start", "t-T0", "Filter", "Exp.", "OT", "Err.", "UL(3sig)", "Observ.", "GCN", "GRB"]
list=["Time", "Tmid", "T0", "t-T0", "t-to"] # rename  to "Time"
oldtonew = Dict(old=> "Time" for old in intersect(names(dataframe), list))
rename(dataframe,oldtonew)

Take a look at the documentation for DataFrames - you can use rename! if you want to do the changes in place (instead of getting a new DataFrame object).

Also - if multiple names will match, you’ll get multiple attempts to rename using the same name (Time): which will also produce an error. If multiple matches are found you might want to adapt the code to produce Time0, Time1 … and so on (or something to ensure you use unique names).

1 Like