列名の変更
準備
1. パッケージのインストール
- R
- Python
- SQL
install.packages("tidyverse")
pip install pandas
なし。
2. パッケージの読み込み
- R
- Python
- SQL
library(tidyverse)
import pandas as pd
なし。
プログラム
R | Python | SQL | |
---|---|---|---|
1. 列名を変更 | Awesome | Awesome | Awesome |
1. 列名を変更
- R
- Python
- SQL
構文
df %>%
rename(
new_col1 = col1,
new_col2 = col2
)
サンプルコード
penguins %>%
rename(
BILL_LENGTH_MM = bill_length_mm,
BILL_DEPTH_MM = bill_depth_mm
)
# # A tibble: 344 × 7
# species island BILL_LENGTH_MM BILL_DEPTH_MM flipper_length_mm body_mass_g sex
# <fct> <fct> <dbl> <dbl> <int> <int> <fct>
# 1 Adelie Torgersen 39.1 18.7 181 3750 male
# 2 Adelie Torgersen 39.5 17.4 186 3800 female
# 3 Adelie Torgersen 40.3 18 195 3250 female
# 4 Adelie Torgersen NA NA NA NA NA
# 5 Adelie Torgersen 36.7 19.3 193 3450 female
# 6 Adelie Torgersen 39.3 20.6 190 3650 male
# 7 Adelie Torgersen 38.9 17.8 181 3625 female
# 8 Adelie Torgersen 39.2 19.6 195 4675 male
# 9 Adelie Torgersen 34.1 18.1 193 3475 NA
# 10 Adelie Torgersen 42 20.2 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows
構文
penguins.rename(columns={'col1': 'new_col1', 'col2': 'new_col2'})
サンプルコード
penguins.rename(columns={'bill_length_mm': 'BILL_LENGTH_MM', 'bill_depth_mm': 'BILL_DEPTH_MM'})
# species island BILL_LENGTH_MM BILL_DEPTH_MM flipper_length_mm body_mass_g sex
# 0 Adelie Torgersen 39.1 18.7 181 3750 male
# 1 Adelie Torgersen 39.5 17.4 186 3800 female
# 2 Adelie Torgersen 40.3 18.0 195 3250 female
# 3 Adelie Torgersen NaN NaN NaN NaN NaN
# 4 Adelie Torgersen 36.7 19.3 193 3450 female
# .. ... ... ... ... ... ... ...
# 339 Chinstrap Dream 55.8 19.8 207 4000 male
# 340 Chinstrap Dream 43.5 18.1 202 3400 female
# 341 Chinstrap Dream 49.6 18.2 193 3775 male
# 342 Chinstrap Dream 50.8 19.0 210 4100 male
# 343 Chinstrap Dream 50.2 18.7 198 3775 female
# [344 rows x 7 columns]
構文
select
* replace(
col1 as new_col1,
col1 as new_col2
)
from penguins;
サンプルコード
select
* replace(
bill_length_mm as BILL_LENGTH_MM,
bill_depth_mm as BILL_DEPTH_MM
)
from penguins;
# | species | island | BILL_LENGTH_MM | BILL_DEPTH_MM | flipper_length_mm | body_mass_g | sex |
# | ------- | --------- | -------------- | ------------- | ----------------- | ----------- | ------ |
# | Adelie | Torgersen | 39.1 | 18.7 | 181 | 3750 | male |
# | Adelie | Torgersen | 39.5 | 17.4 | 186 | 3800 | female |
# | Adelie | Torgersen | 40.3 | 18.0 | 195 | 3250 | female |
# | Adelie | Torgersen | null | null | null | null | null |
# | Adelie | Torgersen | 36.7 | 19.3 | 193 | 3450 | female |
# | Adelie | Torgersen | 39.3 | 20.6 | 190 | 3650 | male |
# | Adelie | Torgersen | 38.9 | 17.8 | 181 | 3625 | female |
# | Adelie | Torgersen | 39.2 | 19.6 | 195 | 4675 | male |
# | Adelie | Torgersen | 34.1 | 18.1 | 193 | 3475 | null |
# | Adelie | Torgersen | 42.0 | 20.2 | 190 | 4250 | null |
# | ... | ... | ... | ... | ... | ... | ... |