Pandas:值替换--replace

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

一、replace的使用

s = Series([1,2,3,4,5,6])
s.replace(6,100)
0      1
1      2
2      3
3      4
4      5
5    100
dtype: int64

二、替换多个值

s.replace([1,2,3,4],100)
0    100
1    100
2    100
3    100
4      5
5      6
dtype: int64

三、不同的值进行不同的替换

s.replace([1,2,3,4],[-1,-2,-3,-4])
0   -1
1   -2
2   -3
3   -4
4    5
5    6
dtype: int64

字典的方式

s.replace({1:-1,2:-2,3:-3,4:-4})
0   -1
1   -2
2   -3
3   -4
4    5
5    6
dtype: int64

猜你喜欢

转载自blog.csdn.net/bqw18744018044/article/details/79963942