0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pandasでobject型の文字列内の記号・特殊文字を置換する

Posted at

Pandasでobject型の文字列内の記号・特殊文字を置換する

Pandasでは文字列内に記号や特殊文字が混在していると、その文字列のデータ型がstr型ではなくobject型として扱われることがあります。

import pandas as pd

prices = ['$14,983.17','$15,321.76','$13,568.23']
df = pd.DataFrame(data=prices)
df
	0
0	$14,983.17
1	$15,321.76
2	$13,568.23

df.dtypes
0    object
dtype: object

object型の文字列内の記号や特殊文字を置換する場合は、replaceメソッドにregex=Trueを指定し、置換する記号・特殊文字を正規表現として指定します。

df = df.replace({'\$': '', ',': ''},regex=True)
df
	0
0	14983.17
1	15321.76
2	13568.23

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?