replace
今天学到的,把Dataframe一列里的字符串数据分别替换成不同的数字或其他的替换单个字符:
1、把A替换成X
replace('A','X')
替换多个字符时
1、把A,B,C 都替换为E
replace(['A','B','C'],'X')
2、把A替换成7,把C替换成3
replace({'A':7,'C':3})
实例证明:
import pandas as pd
import numpy as np
data = {"id":[0,1,2,3,4,5,6,7,8,9,10],"pc":["qi","yi","san","qi","qi","qi","qi","qi","san","yi","yi"],"num":[1,2,3,1,1,2,3,1,2,6,4]}
df = pd.DataFrame(data)
df['pc'] = df['pc'].replace('san',3)
#df['pc'] = df['pc'].replace(['qi','yi','san'],3)
#df['pc'] = df['pc'].replace({'qi':7,'yi':1,'san':3})
print(df)
结果输出为:
第一个
id pc num
0 0 qi 1
1 1 yi 2
2 2 3 3
3 3 qi 1
4 4 qi 1
5 5 qi 2
6 6 qi 3
7 7 qi 1
8 8 3 2
9 9 yi 6
10 10 yi 4
第二个
id pc num
0 0 3 1
1 1 3 2
2 2 3 3
3 3 3 1
4 4 3 1
5 5 3 2
6 6 3 3
7 7 3 1
8 8 3 2
9 9 3 6
10 10 3 4
第三个
id pc num
0 0 7 1
1 1 1 2
2 2 3 3
3 3 7 1
4 4 7 1
5 5 7 2
6 6 7 3
7 7 7 1
8 8 3 2
9 9 1 6
10 10 1 4