Rename all columns of a dataframe removing all characters, if any, before the last slash

Advertisements I have a dataframe where some, but not all, columns start with weird prefixes. For example, ot1/pr1/cp1/dkjsakhka, gt2/pr1/fl1/ct4/rmt12/dasljdals, etc. I want to rename all columns which have slashes in their names by removing all the characters before the last slash, e.g. ot1/pr1/cp1/dkjsakhka -> dkjsakhka, gt2/pr1/fl1/ct4/rmt12/dasljdals -> dasljdals, while leaving the other column names untouched.… Read More Rename all columns of a dataframe removing all characters, if any, before the last slash

What is the purpose of the tilde operator when used to append the paste0() function in R?

Advertisements I am unsure as to how exactly the following line of code works, given that without the tilde… betterLifeIndicators <- betterLifeIndicators |> rename_with(paste0(.x, ")")) it doesn’t work, citing that the object ‘.x’ is not found. Yet when adding the tilde in front of paste0… betterLifeIndicators <- betterLifeIndicators |> rename_with(~paste0(.x, ")")) it works perfectly. I… Read More What is the purpose of the tilde operator when used to append the paste0() function in R?

pandas index rename using dictionary

Advertisements Is there a version of this but using a dictionary instead of a list? Following the example in the link what I want to do is idx.rename({‘kind’:’species’}). If it matters, this is how my data frame looks like: k_CFD (%) k_CFD (%) error run_name CFD 20230104124758_TI107_HDO6034-MS_WithInterpolati… DUT 68.529412 9.576598 MCP-PMT 72.058824 21.711526 20230104124758_TI107_HDO6034-MS_WithInterpolati… DUT… Read More pandas index rename using dictionary

Renaming characters in multiple files in a folder

Advertisements I’m trying to rename files in a folder with import os. Folder I want to replace the ‘SN’ in all these files with ‘NG’. I have tried with the code ` filenames = os.listdir(serial_dir) #serial_dir is the right path for filename in filenames: dst = filename.replace(‘SN’, ‘NG’) os.rename(filename, dst) ` I have tested by… Read More Renaming characters in multiple files in a folder

Rename vars with numeric names

Advertisements Say I have a dataframe with variables that have numeric names that I want to rename by adding character prefixes. country <- c("country","US","France") var1 <- c(234234,234234,1233124) var2 <- c(34534,25674,123124) df <- data.frame(country,var1,var2) %>% rename(‘2021′ = var1,’2022’ = var2) Country 2021 2022 country 234234 34534 US 234234 25674 France 1233124 123124 Example table is weird,… Read More Rename vars with numeric names

How to change the names of columns of a list of dataframes?

Advertisements I have this list of dataframes created as follows : df = data.frame(x = c(1,0,0,0,1,1,1,NA), y = c(2,2,2,2,3,3,2,NA), z = c(1:7,NA), m = c(1,2,3,1,2,3,1,NA) ) df$x = factor(df$x) df$y = factor(df$y) df$m = factor(df$m) l1 = list(df$x,df$y,df$m) l2 = lapply(l1,table) l3 = lapply(l2,as.data.frame) l3 The output is as follows : [[1]] Var1 Freq 1… Read More How to change the names of columns of a list of dataframes?