LoginSignup
0
0

More than 1 year has passed since last update.

【Python】データ補正(全半角文字の統一、置換)

Last updated at Posted at 2022-02-25

備忘録

計算環境

macOS Monterey 12.2
Python 3.8.2
Panas 1.4.0

ファイル

元ファイル(汚いデータ)
csv_file_01.csv 

名前 Name 都市 登録日
佐藤 sato 東_京 2022-01-05
鈴木 suzuki 大_阪 2022-02-01

csv_file_02.csv

名前 Name 都市 登録日
高橋 Takahashi 大_阪 2022-01-20
田中 Tanaka 東_京 2022-02-09
伊藤 sato 名古屋 2022-01-17
渡辺 watanabe 東京 2022-01-17

ゴール(整理されたデータに補正)
csv_file_new.csv

名前 Name 都市 登録日
佐藤 SATO 東京 2022-01-05
鈴木 SUZUKI 大阪 2022-02-01
高橋 TAKAHASHI 大阪 2022-01-20
田中 TANAKA 東京 2022-02-09
伊藤 ITO 名古屋 2022-01-17
渡辺 WATANABE 東京 2022-01-17

課題

  1. Nameに大/小文字が混在
  2. 都市に'_'を含む

コード


import pandas as pd

# csv, excelファイルの読み込み
csv_data_01 = pd.read_csv('csv_file_01.csv')
csv_data_02 = pd.read_csv('csv_file_02.csv')

# データを縦方向に結合
csv_data = pd.concat([csv_data_01, csv_data_02], ignore_index=True)

# 課題1の解消:大文字に統一
csv_data['Name'] = csv_data['Name'].str.upper()

# 課題2の解消:"_"の削除
csv_data['都市'] = csv_data['都市'].str.replace("_", "")

# csvの書き出し
csv_data.to_csv('csv_file.csv', index=True)


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0