Pandas 
 keyboard_arrow_down 655 guides
 chevron_leftSeries Cookbook
Appending values to a SeriesApplying a function to SeriesBinning values in a SeriesChanging data type of SeriesChecking if a value is NaN in Pandas SeriesChecking if all values are NaN in SeriesChecking if all values in a Series are uniqueChecking if Series has missing valuesConverting Python list to SeriesConverting Series of lists into DataFrameConverting Series to a Numpy arrayConverting Series to Python listCounting frequency of values in SeriesCreating a Series of zeroesCreating a Series with constant valueFiltering strings based on length in SeriesFiltering values of a SeriesGetting frequency counts of values in intervalsGetting index of largest valueGetting index of smallest valueGetting index of value in SeriesGetting integer index of largest valueGetting integer index of smallest valueGetting integer index of value in SeriesGetting intersection of SeriesGetting length of each string in SeriesGetting list of integer indices where value is boolean True in SeriesGetting the index of the nth value in SeriesGetting the most frequent value in SeriesGetting value of Series using integer indexGrouping Series by its valuesHandling error - "Truth value of a Series is ambiguous"Inverting a Series of booleansRemoving missing values from a SeriesRemoving substrings from strings in a SeriesRemoving values from SeriesResetting index of SeriesSorting values in a SeriesSplitting strings based on spaceStripping leading and trailing whitespaceTaking the floor or ceiling of values in SeriesUsing index.get_loc(~) for multiple values
  check_circle
 Mark as learned thumb_up
 4
 thumb_down
 1
 chat_bubble_outline
 0
 Comment  auto_stories Bi-column layout 
 settings
 Removing substrings from strings in a Series in Pandas
 schedule Aug 10, 2023 
 Last updated  local_offer 
 Tags Python●Pandas
  tocTable of Contents
 expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
   Start your free 7-days trial now!
Removing substrings from string in Pandas Series
To remove a substring from each string in a Pandas Series, use the str.replace(~) method:
        
        
            
                
                
            
            0    a1    bdtype: object
        
    By default, regex=True, which means that the pattern (first argument) is treated as a regular expression:
        
        
            
                
                
                    s.str.replace("[aA]", "")   # regex=True
                
            
            0    b1    bdtype: object
        
    You can also toggle case-sensitivity for the matches using the case parameter:
        
        
            
                
                
                    s.str.replace("a", "", case=False)
                
            
            0    b1    bdtype: object
        
    Removing substrings from strings in Pandas DataFrame
Consider the following DataFrame:
        
        
            
                
                
            
               col1  col20  aAA   aB1  Ab    AAB
        
    To remove the substring AA from column col1:
        
        
            
                
                
                    df["col1"].str.replace("AA", "")   # returns a Series
                
            
            0    a1    bName: col1, dtype: object
        
    If you want to replace col1 with this Series, then perform the following:
        
        
            
                
                
                    df["col1"] = df["col1"].str.replace("A", "")df
                
            
               col1  col20   a     aB1   b     AA
        
    Note that str.replace is a function defined only for Pandas Series (columns) and not for DataFrames, so make sure to extract the individual columns as a Series first.
Removing substrings from strings for multiple Pandas DataFrame columns
To removing substrings from strings for multiple columns:
        
        
            
                
                
                    for column in ["col1","col2"]:    df[column] = df[column].str.replace("AA", "")
df
                
            
               col1  col20   a     aB1   b     B
        
      Published by Isshin Inada
 Edited by 0 others
 Did you find this page useful?
 thumb_up
 thumb_down
 Comment
 Citation
  Ask a question or leave a feedback...
 thumb_up
 4
 thumb_down
 1
 chat_bubble_outline
 0
 settings
 Enjoy our search
 Hit / to insta-search docs and recipes!