MENU
  • ホーム
  • Contact Us
Hayashi's discovery-oriented learning | 地域研究と分析の林
地域研究と分析の林
  • ホーム
  • Contact Us
地域研究と分析の林
  • ホーム
  • Contact Us
  1. ホーム
  2. データ前処理と即席可視化
  3. よく使うデータフレーム操作

よく使うデータフレーム操作

行名、列名の変更

df.rename(columns={'1列目(旧名)': '1列目(新名)', '2列目(旧名)': '2列目(新名)'}, index={'1行目(旧名)': '1行目(新名)'}, inplace=True)
# 列名の変更
colnames(df) <- c('1列目(新名)', '2列目(新名)')
# 行名の変更
rownames(df) <- c('1行目(新名)')

行削除・列削除

# 1行のみ
df.drop(index='削除対象1行目', inplace=True)

# 複数行(名称指定)
df.drop(index=['削除対象1行目', '削除対象2行目', '削除対象3行目'], inplace=True)

# 複数行(行番号指定)
df.drop(df.index[[1, 2, 7]], inplace=True)

# 1列のみ
df.drop(columns='削除対象1列目', inplace=True)

# 複数列(名称指定)
df.drop(columns=['削除対象1列目', '削除対象2列目'], inplace=True)

# 複数列(列番号指定)
df.drop(columns=df.columns[[1, 2]], inplace=True)
# 1行のみ
df <- df[-which(rownames(df) == '削除対象1行目'), ]

# 複数行(名称指定)
df <- df[!(rownames(df) %in% c('削除対象1行目', '削除対象2行目', '削除対象3行目')), ]

# 複数行(行番号指定)
df <- df[-c(2, 3, 8), ]

# 1列のみ
df <- df[, !(colnames(df) %in% '削除対象1列目')]

# 複数列(名称指定)
df <- df[, !(colnames(df) %in% c('削除対象1列目', '削除対象2列目'))]

# 複数列(列番号指定)
df <- df[, -c(2, 3)]

インデックスをリセット

df.reset_index(inplace=True, drop=True)
df <- df[order(rownames(df)), ]
rownames(df) <- NULL

特定の列をインデックスに

df_name.set_index('新インデックス希望列名')
df <- df_name
rownames(df) <- df_name$新インデックス希望列名
スポンサーリンク
新着記事
  • 鳳派出所の風景(後編)
  • 鳳派出所の風景(中編)
  • 鳳派出所の風景(前編)
  • ホーム
  • Contact Us

© 地域研究と分析の林.