3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pandasひらがな→カタカナ変換

Posted at

pandasを利用してCSVを編集する場合、特定の列全体の値を変換したいです。
ネット上で調べて半角文字→全角文字へ変更する処理がありますが、ひらがな→カタカナ変換する処理が見つかりませんでした。
changeKanaのメソッドを作って、maketrans経由でchangeKanaを呼ぶように実装しました。

df = pandas.read_csv(file, encoding='cp932', engine='python', dtype=object)

半角→全角
df[列名] = df[列名].str.translate(str.maketrans({chr(0x0021 + i): chr(0xFF01 + i) for i in range(94)}))
ひらがな→カタカナ
df[列名] = df[列名].str.translate(str.maketrans({chr(x):changeKana(chr(x)) for x in range(12353, 12436)}))

# パターン1:半角→全角
# パターン2:ひらがな→カタカナ
def changeKana(in_Word):
    # 本当は半角→全角の変更は不要
    changeWord = jaconv.h2z(in_Word)
    changeWord = jaconv.hira2kata(changeWord)

    return changeWord
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?