Q&A 7 How do you rename column names in Python and R?
7.1 Explanation
When working with the same dataset in both Python and R, you may encounter slight differences in column names — such as capitalization or spacing. To avoid confusion and ensure consistency across your analysis, it’s best to standardize the column names.
In this guide, we’ll rename the columns to lowercase with underscores:
sepal_length
sepal_width
petal_length
petal_width
species
After renaming, we’ll save the final, standardized dataset as data/iris.csv
, which will be used consistently throughout the rest of the guide.
7.2 Python Code
import pandas as pd
# Using seaborn version of the iris dataset
df1 = pd.read_csv("data/iris_seaborn.csv")
print("Original column names from seaborn version:", df1.columns.tolist())
# Rename columns
df1.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width", "species"]
# Save standardized version
df1.to_csv("data/iris.csv", index=False)
print("Saved standardized dataset from seaborn version as 'data/iris.csv'")
Original column names from seaborn version: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
Saved standardized dataset from seaborn version as 'data/iris.csv'
7.3 R Code
library(readr)
library(dplyr)
# Option 1: If your dataset already has lowercase column names (e.g., from iris_rbase.csv)
df <- read_csv("data/iris_rbase.csv")
# Set standardized column names directly
colnames(df) <- c("sepal_length", "sepal_width", "petal_length", "petal_width", "species")
# Save standardized dataset
write_csv(df, "data/iris.csv")
cat("Saved standardized dataset from iris_rbase.csv as data/iris.csv\n")
Saved standardized dataset from iris_rbase.csv as data/iris.csv
# Option 2: If your dataset has capitalized column names (e.g., from iris_rbase.csv)
df <- read_csv("data/iris_rbase.csv")
# Rename columns using dplyr for consistency
df <- df %>%
rename(
sepal_length = Sepal.Length,
sepal_width = Sepal.Width,
petal_length = Petal.Length,
petal_width = Petal.Width,
species = Species
)
# Save standardized dataset
write_csv(df, "data/iris.csv")
cat("Saved standardized dataset from iris_rbase.csv as data/iris.csv\n")
Saved standardized dataset from iris_rbase.csv as data/iris.csv
✅ From this point forward, we’ll use
data/iris.csv
as the unified, clean dataset for all Python and R examples in the guide.