Change column names in Pandas Dataframe from a list
Using rename is a formally more correct approach. You just have to provide a dictionary that maps your current columns names to the new ones (thing that will guarantee expected results even in case of misplaced columns)
new_names = {'A':'NaU', 'B':'MgU', 'C':'Alu', 'D':'SiU'}
df.rename(index=str, columns=new_names)
Notice you can provide entries for the sole names you want to substitute, the rest will remain the same.
Change column names in list of dataframes
Try this, loop through data.frames, update column name:
# dummy list
my_list <- list(one = data.frame(a = 1:5, b = 1:5), two = data.frame(a = 1:5, b = 1:5))
my_list_updated <-
lapply(names(my_list), function(i){
x <- my_list[[ i ]]
# set 2nd column to a new name
names(x)[2] <- i
# return
x
})
my_list_updated
# [[1]]
# a one
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 4
# 5 5 5
#
# [[2]]
# a two
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 4
# 5 5 5
Changing Column Names in a List of Data Frames in R
With lapply you can do it as follows.
Create sample data:
df1 <- data.frame(A = 1, B = 2, C = 3)
df2 <- data.frame(X = 1, Y = 2, Z = 3)
dfList <- list(df1,df2)
colnames <- c("USAF","WBAN","YR--MODAHRMN")
Then, lapply over the list using setNames
and supply the vector of new column names as second argument to setNames
:
lapply(dfList, setNames, colnames)
#[[1]]
# USAF WBAN YR--MODAHRMN
#1 1 2 3
#
#[[2]]
# USAF WBAN YR--MODAHRMN
#1 1 2 3
Edit
If you want to assign the data.frames back to the global environment, you can modify the code like this:
dfList <- list(df1 = df1, df2 = df2)
list2env(lapply(dfList, setNames, colnames), .GlobalEnv)
Add file names to columns in a list of dataframes
It can be:
#Code
tucson_function<- function (x) {
df <- read.csv(x)
tag <- sub('\\.csv$', '', x)
df$tag <- tag
}
Or:
#Code
tucson_function<- function (x) {
df <- read.csv(x)
tag <- sub('\\.csv$', '', x)
names(df) <- paste0(tag,'.',names(df))
return(df)
}
Using lapply to set column names for a list of data frames?
It seems you want to update the original dataframes. In that case, your list MUST be named. ie check the code below.
List <- list(a = a, b = b, c = c, d = d)
list2env(lapply(List, setNames, nm = headers), globalenv())
Now if you call a
you will note that it has been updated.
Change column names of data frames stored in a list by condition
dplyr::rename_with()
applies a function to each column name. In that function we can check if the name contains “abund” or “individuals” with grepl()
and then those columns get renamed. The columns that don’t contain the strings we are looking for technically also get renamed, but they receive their old name again, so nothing is changed there.
library(dplyr)
library(purrr)
map(lst, ~ rename_with(., ~ ifelse(
grepl("abund|individuals", .), "abundance", .
)))
#> $spiders
#> plot abundance habitat
#> 1 1 1 forest
#> 2 2 4 forest
#> 3 3 8 forest
#>
#> $bugs
#> plot abundance
#> 1 1 1
#> 2 2 4
#> 3 3 8
#>
#> $birds
#> plot abundance habitat
#> 1 1 1 forest
#> 2 2 4 forest
#> 3 3 8 forest
#> 4 1 1 visual
#> 5 2 4 visual
#> 6 3 8 visual
Instead of using tidyverse style anonymous functions we can use the new
base R anonymous function style in order to make the code a bit more comprehensible.
map(lst, \(df) rename_with(df, \(name) ifelse(
grepl("abund|individuals", name), "abundance", name
)))
#> $spiders
#> plot abundance habitat
#> 1 1 1 forest
#> 2 2 4 forest
#> 3 3 8 forest
#>
#> $bugs
#> plot abundance
#> 1 1 1
#> 2 2 4
#> 3 3 8
#>
#> $birds
#> plot abundance habitat
#> 1 1 1 forest
#> 2 2 4 forest
#> 3 3 8 forest
#> 4 1 1 visual
#> 5 2 4 visual
#> 6 3 8 visual
Add new column name to a list of data frames from a part of the file name using lapply
Name the list of filenames using setNames()
, then use the .id
argument in bind_rows()
, which adds a column containing list names.
library(tidyverse)
library(readxl)
files <- list.files(path ="Users/Desktop/week", pattern = "*.xlsx", full.names= T) %>%
setNames(nm = .) %>%
lapply(read_excel, sheet =4, skip =39) %>%
bind_rows(.id = "Week") %>%
mutate(Week = str_extract(Week, "wk\\d+"))
You could also combine the iteration and row-binding steps using purrr::map_dfr()
:
files <- list.files(path ="Users/Desktop/week", pattern = "*.xlsx", full.names= T) %>%
setNames(nm = .) %>%
map_dfr(read_excel, sheet = 4, skip = 39, .id = "Week") %>%
mutate(Week = str_extract(Week, "wk\\d+"))
How to change column names in pandas Dataframe using a list of names?
you could use
df.columns = ['Leader', 'Time', 'Score']
Related Topics
How to Get Dimnames in Xtable.Table Output
How to Print the Name of Current Row When Using Apply in R
Plot Table Objects with Ggplot
R: Ggplot2: Adding Count Labels to Histogram with Density Overlay
How to Flatten R Data Frame That Contains Lists
Ggplot2: Cannot Color Area Between Intersecting Lines Using Geom_Ribbon
How to Search for a String in One Column in Other Columns of a Data Frame
How to Prevent Rplots.Pdf from Being Generated
Enriching a Ggplot2 Plot with Multiple Geom_Segment in a Loop
Mathematical Expression in Axis Label
Re- Installing R Linux Ubuntu: Unmet Dependencies R
R Shiny: How to Write Loop for Observeevent
Error When Exporting Dataframe to Text File in R
Adding a Ranking Column to a Dataframe