replace()
함수 사용 가능df.replace(
to_replace=None,
value=None,
inplace=False,
limit=None,
regex=False,
method='pad'
)
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [4, 1, 8]})
# X Y
# 0 1 4
# 1 2 1
# 2 3 8
replaced_df=df.replace(1, 5)
# X Y
# 0 5 4
# 1 2 5
# 2 3 8
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [4, 1, 8]})
# X Y
# 0 1 4
# 1 2 1
# 2 3 8
replaced_df=df.replace([1,2,3],[1,4,9])
# X Y
# 0 1 4
# 1 4 1
# 2 9 8
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [3, 1, 8]})
# X Y
# 0 1 3
# 1 2 1
# 2 3 8
replaced_df=df.replace({1:10,3:30})
# X Y
# 0 10 30
# 1 2 10
# 2 30 8
import pandas as pd
df = pd.DataFrame({'X': ["zeppy", "amid", "amily"],
'Y': ["xar", "abc", "among"]})
# X Y
# 0 zeppy xar
# 1 amid abc
# 2 amily among
df.replace(to_replace=r'^ami.$', value='song', regex=True,inplace=True)
# X Y
# 0 zeppy xar
# 1 song abc
# 2 amily among
pandas 공식 document를 참고하였음.