1. pandas.replace() Introduce
2. Single value replacement
2.1 Global replacement
2.2 Selected condition replacement
3. Multivalued substitution
3.1 Multiple values replace the same value
3.2 Multiple values replace different values
4. Fuzzy query replacement
5. Missing value replacement
5.1 method Usage of ( forward / Post filling )
5.2 limit Usage of ( Limit the maximum fill interval )
Add : Use instance code
summary
1. pandas.replace() Introducepandas.Series.replace Official documents
Series.replace(to_replace=None, value=NoDefault.no_default, inplace=False, limit=None, regex=False, method=NoDefault.no_default)
to_replace: Values that need to be replaced
value: The replaced value
inplace: Whether to change on the original data sheet , Default inplace=False
limit: Maximum dimension gap filled forward or backward , Used to fill in missing values
regex: Fuzzy query , Used for regular expression lookup , Default regex=False
method: fill style , Used to fill in missing values (The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.)pad: Fill forward
ffill: Fill forward
bfill: Fill back
Example
2. Single value replacement 2.1 Global replacementdf.replace(1, 10)
2.2 Selected condition replacement df['attr_1'].replace(' scene . season . winter ', ' winter ', inplace=True)
3. Multivalued substitution 3.1 Multiple values replace the same value df.replace([3, 11, 137], 4)
3.2 Multiple values replace different values list List
df.replace([3, 11, 137, 1], [1, 111, 731, 10])
Dictionary mapping
# Modify different columns df.replace({' scene . Ordinary sports . running ':' running ', 11:100})
# Modify the same column df.replace({'attr_1':{' scene . Ordinary sports . running ':' running ', ' scene . Outdoor leisure . Climbing the mountain ':' Climbing the mountain '}})
4. Fuzzy query replacement df.replace(' scene .','', regex=True)df.replace(regex=' scene .', value=' ')
df.replace(regex={' scene .': '', ' programme .':''})df.replace(regex=[' scene .', ' programme .'], value='')
You can do that
df['Attr_B'] = df['Attr_B'].str.replace(' The jacket ', ' overcoat ')df
5. Missing value replacement 5.1 method Usage of ( forward / Post filling )Example
Fill forward ( Fill in with the value of his previous line )
s.replace(np.nan, method='pad')s.replace(np.nan, method='ffill')
Fill back ( Fill in with the value of the next line )
s.replace(np.nan, method='bfill')
5.2 limit Usage of ( Limit the maximum fill interval )When multiple null values are connected ,limit Fill in a few
Example
s.replace(np.nan, method='ffill', limit=1)
s.replace(np.nan, method='ffill', limit=2)
Add : Use instance code #Series Object value substitution s = df.iloc[2]# Get row index as 2 data # Single value replacement s.replace('?',np.nan)# use np.nan Replace ?s.replace({'?':'NA'})# use NA Replace ?# Multivalued substitution s.replace(['?',r'$'],[np.nan,'NA'])# List value substitution s.replace({'?':np.nan,'$':'NA'})# Dictionary mapping # It is similar to the missing value filling method s.replace(['?','$'],method='pad')# Fill forward s.replace(['?','$'],method='ffill')# Fill forward s.replace(['?','$'],method='bfill')# Fill back #limit Parameter controls the number of fills s.replace(['?','$'],method='bfill',limit=1)#DataFrame Object value substitution # Single value replacement df.replace('?',np.nan)# use np.nan Replace ?df.replace({'?':'NA'})# use NA Replace ?# Specify single valued substitution by column df.replace({'EMPNO':'?'},np.nan)# use np.nan Replace EMPNO In the column ?df.replace({'EMPNO':'?','ENAME':'.'},np.nan)# use np.nan Replace EMPNO In the column ? and ENAME in .# Multivalued substitution df.replace(['?','.','$'],[np.nan,'NA','None'])## use np.nan Replace ? use NA Replace . use None Replace $df.replace({'?':'NA','$':None})# use NA Replace ? use None Replace $df.replace({'?','$'},{'NA',None})# use NA Replace ? use None Replace $# Regular substitution df.replace(r'\?|\.|\$',np.nan,regex=True)# use np.nan Replace ? or . or $ Original character df.replace([r'\?',r'\$'],np.nan,regex=True)# use np.nan Replace ? and $df.replace([r'\?',r'\$'],[np.nan,'NA'],regex=True)# use np.nan Replace ? use NA Replace $ Symbol df.replace(regex={r'\?':None})#value The parameter shows the transfer df.replace(regex=[r'\?|\.|\$'],value=np.nan)# use np.nan Replace ? or . or $ Original character
summary This is about Python pandas.replace This is the end of the article on usage , More about Python pandas.replace Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
1、 Engineering structure ( pac
Recently, I received a network