pandas39 replace替换数值( tcy)

replace-替换2019/1/4

1.函数: 

df.replace(to_replace=None, value=None, inplace=False,
limit=None, regex=False, method='pad') #value替换给定的值to_replace
# replace动态替换不同于.loc更新(需指定要更新的位置)

参数:
to_replace:str,regex,list,dict,Series,int,float或None#将被替换的值
   1)numeric,str或regex:等于或匹配的值将被替换
   2)str,regex或numeric列表:
      # to_replace和value都是列表长度必须相同
      # regex=True两列表中字符串都被解释为正则表达式,否则将直接匹配。
   3)字典:
      # 直接替换:to_replace= {'a': 'b', 'y': 'z'},value=None
      # 指定列:{'A': 0, 'B': 5}, value # 用值替换A,B列中的数值
      # 嵌套的典:{'A': {'b':np.nan}}) # A列b被None替换,b可是RE,A不可以;

value=None:标量,字典,列表,str,正则表达式;#要替换的值
inplace=False  #True就地修改
limit=None:int#向前或向后填充的数量

regex=False:bool或与to_replace相同的类型;正则表达式
   # to_replace=r'^ba.$', value=str, regex=True
   # to_replace={'A': r'^ba.$'}, value={'A': str}, regex=True
   # regex=r'^ba.$', value=str #regex为字符串to_replace必为None
   # regex={r'^ba.$':str, 'foo':str}
   # regex=[r'^ba.$', str], value=str

method='pad':{'pad','ffill','bfill',None}#用前面或后面的值替换

2.实例1:  

# 实例1.1:to_replace=标量;value=标量
s = pd.Series([0, 1, 2, 3, 4])
df = pd.DataFrame({'A': [0,1,2,3,4], 'B': [5,6,7,8,9],'C': ['a','b','c','d','e']})

s.replace(0, 5)  # 用5替换0
df.replace(0, 5) # 用5替换0

# 实例1.2:to_replace=列表;value=标量或列表
df.replace([0, 1, 2, 3], 4)            # 用4替换0,1,2,3,4
df.replace([0, 1, 2, 3], [4, 3, 2, 1]) # 4,3,2,1分别替换0,1,2,3

# 实例1.3:to_replace=dict;value=None或标量
df.replace({0: 10, 1: 100})         # 10替换0,100替换1
df.replace({'A': 0, 'B': 5}, 100)   # A列0,B列5都用100替换
df.replace({'A': {0: 100, 4: 400}}) # A列0被100替换,4被400替换

s.replace([1, 2], method='bfill')   # 后项替换即用3替换1,2

df = pd.DataFrame({'A': [1,2,3,4,5,6], 'B': [-1,10,11,12,12,14]})
df.replace({1: 10, 2000: 60, 30: -30, 40: -40})
# 实例2:正则表达式:  
df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],'B': ['abc', 'bar', 'xyz']})

result1=df.replace(to_replace=r'^ba.$', value='new', regex=True)#结果同下
result1=df.replace(regex=r'^ba.$', value='new')                 #等价
result2=df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)
result3=df.replace(regex={r'^ba.$':'new', 'foo':'xyz'})
result4=df.replace(regex=[r'^ba.$', 'foo'], value='new')

# result1 result2 result3 result4
  A   B        A B        A B        A B
0 new abc  0 new abc  0 new abc  0 new abc
1 foo new  1 foo bar  1 xyz new  1 new new
2 bait xyz 2 bait xyz 2 bait xyz 2 bait xyz
# 实例3:替换bool或datetime64数据类型必须匹配 
df = pd.DataFrame({'A': [True, False, True],'B': [False, True, False]})
df.replace({'a string': 'new value', True: False}) # 错误数据类型不匹配
#实例4 method 
s = pd.Series([0, 1, 2, 3, 4])
result1=s.replace([2,3],method='pad')
result1=s.replace([2,3],method='ffill')
result2=s.replace([2,3],method='bfill')

# s      result1   result2
0 0    0 0       0 0
1 1    1 1       1 1
2 2    2 1       2 4
3 3    3 1       3 4
4 4    4 4       4 4
dtype: int64 dtype: int64 dtype: int64
#实例5 
s = pd.Series([1, 'a', 'a', 'b', 'a'])

result1=s.replace('a', None)
result1=s.replace(to_replace='a', value=None, method='pad') #等价
result2=s.replace({'a': None})
result2=s.replace(to_replace={'a': None}, value=None, method=None)#等价

# s             result1       result2
0 1           0 1           0 1
1 a           1 1           1 None
2 a           2 1           2 None
3 b           3 b           3 b
4 a           4 b           4 None
dtype: object dtype: object dtype: object

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/85791555