'abc' というストリングを総て、'01234' に置き換える方法
プログラム
str_replace.py
#! /usr/bin/python
#
# str_replace.py
#
# Jul/14/2024
#
import pandas as pd
#
data = {'col1': ['abc', 'xyz', 'abc', 'pqr', '923'],
'col2': ['abc', 'def', 'abx', 'ghi', '654'],
'col3': ['abc', 'abc', 'abc', 'abc', '231']}
df = pd.DataFrame(data)
print(df)
#
str_src = 'abc'
str_target = '01234'
df = df.map(lambda x: str_target if x == str_src else x)
print(df)
実行結果
$ ./str_replace.py
col1 col2 col3
0 abc abc abc
1 xyz def abc
2 abc abx abc
3 pqr ghi abc
4 923 654 231
col1 col2 col3
0 01234 01234 01234
1 xyz def 01234
2 01234 abx 01234
3 pqr ghi 01234
4 923 654 231