Rename multiple columns by names
setnames
from the data.table
package will work on data.frame
s or data.table
s
library(data.table)
d <- data.frame(a=1:2,b=2:3,d=4:5)
setnames(d, old = c('a','d'), new = c('anew','dnew'))
d
# anew b dnew
# 1 1 2 4
# 2 2 3 5
Note that changes are made by reference, so no copying (even for data.frames!)
A quick way to rename multiple columns with unique names using dplyr
We can use !!!
with rename
by passing a named vector
library(dplyr)
library(stringr)
df1 <- df %>%
rename(!!! setNames(names(df)[-1], str_c(month.abb[1:4], 17)))
-output
df1
# A tibble: 2 x 5
# Product Jan17 Feb17 Mar17 Apr17
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 Eggs 35.8 39.2 40.1 41.1
#2 Chicken 36.8 39.8 43.4 41.3
Or use rename_with
df %>%
rename_with(~str_c(month.abb[1:4], 17), -1)
If the column names should be converted to Date
formatted
nm1 <- format(as.Date(as.numeric(names(df)[-1]), origin = '1896-01-01'), '%b%y')
df %>%
rename_with(~ nm1, -1)
# A tibble: 2 x 5
# Product Jan17 Feb17 Mar17 Apr17
# <chr> <dbl> <dbl> <dbl> <dbl>
#1 Eggs 35.8 39.2 40.1 41.1
#2 Chicken 36.8 39.8 43.4 41.3
pandas rename multiple columns using regex pattern
You could use a regex that matches the "US-" at the beginning like this:
df.columns = df.columns.str.replace("^US-", "", regex=True)
It replaces the matching "US-" with an empty string.
Also, if you know the columns that you want to transform you could apply slicing on their names to remove the first 3 characters:
df.columns = df.columns.str.slice(3)
Of course, this will affect columns that do not match your condition (i.e. do not begin with "US-")
Renaming column names in Pandas
Just assign it to the .columns
attribute:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
Rename multiple columns by extracting contents in parentheses for a list of dataframes
Using lapply
to manipulate the elements of the list dfs
, we can use some regex
to do the job.
dfs <- lapply(dfs, function(x) {
col_num <- grep('pred|error', colnames(x))
colnames(x)[col_num] <- gsub('.*\\(|\\)', '', colnames(x)[col_num]); x
})
Output
> dfs
[[1]]
id 2021-10(actual) 2021-11(actual) 2021-12(actual) pred error
1 M0000607 8.9 7.3 6.1 6.113632 0.7198461
2 M0000609 15.7 14.8 14.2 14.162432 0.1544640
3 M0000612 5.3 3.1 3.5 3.288373 1.2259926
[[2]]
id 2021-09(actual) 2021-10(actual) 2021-11(actual) pred error
1 M0000607 10.3 8.9 7.3 8.352098 1.9981091
2 M0000609 17.3 15.7 14.8 13.973182 0.4143733
3 M0000612 6.4 5.3 3.1 3.164683 0.3420726
[[3]]
id 2021-08(actual) 2021-09(actual) 2021-10(actual) pred error
1 M0000607 12.6 10.3 8.9 9.619846 0.9455678
2 M0000609 19.2 17.3 15.7 15.545536 4.8832500
3 M0000612 8.3 6.4 5.3 6.525993 1.2158196
R rename multiple columns with wildcard with rename_with()
We can use rename_all
with str_replace
library(dplyr)
library(stringr)
tbl2 <- tbl1 %>%
rename_all(~ str_replace_all(str_replace(., '^[^_]+_(.*)_(.)[^.]+$', "\\1\\2"), '_', "-"))
-output
tbl2
# A tibble: 0 x 3
# … with 3 variables: `BBB1-P1E` <dbl>, `BBB2-P2E` <dbl>, `BBB2-P3E` <dbl>
data
tbl1 <- structure(list(AAA_BBB1_P1_Elev = numeric(0), AAA_BBB2_P2_Elev = numeric(0),
AAA_BBB2_P3_Elev = numeric(0)), row.names = integer(0), class = c("tbl_df",
"tbl", "data.frame"))
Rename multiple columns by index using an iterative name in pandas
You can iterate over it to change the names. Change the range to get your desired range like ...numerate(df.columns[
1:3])..
names = ['Dog','Cat']
for index, column_name in enumerate(df.columns[1:3]):
df.rename(columns={column_name: f'Q{names[index]}{index+1}'}, inplace=True)
output
A QDog1 QCat2 D E
0 a 4 7 1 a
1 b 5 8 3 a
2 c 4 9 5 a
3 d 5 4 7 b
4 e 5 2 1 b
5 f 4 3 0 b
How to change multiple column names at one time?
A simple == check, checks for eqaulity. So 1 == 1 is true. However if you want to check multiple values, you have to use a different operator (I know, weird right?!) essentially in the line names(dmodel112)= c("name1, "name2", "name3")
you try to compare an entire vector (the colnames of your dataframe) against a vector.
Beter is names(dmodel112) %in% c("name1, "name2", "name3")
that way you check against every option in the vector c("name1", "name2", etc)
Related Topics
Dummify Character Column and Find Unique Values
Unlist Data Frame Column Preserving Information from Other Column
Read All Files in a Folder and Apply a Function to Each Data Frame
Dplyr Summarise: Equivalent of ".Drop=False" to Keep Groups With Zero Length in Output
How to Position Two Legends Independently in Ggplot
Generate N Random Integers That Sum to M in R
Read a Text File in R Line by Line
Order of Operator Precedence When Using ":" (The Colon)
Latitude Longitude Coordinates to State Code in R
Workflow For Statistical Analysis and Report Writing
Efficient Way to Rbind Data.Frames With Different Columns
How to Quickly Form Groups (Quartiles, Deciles, etc) by Ordering Column(S) in a Data Frame
Plot With Conditional Colors Based on Values in R
How to Subtract/Add Days From/To a Date
Remove Parentheses and Text Within from Strings in R
Finding Percentage in a Sub-Group Using Group_By and Summarise