How to parse through complex season and episode formatting in Python Pandas
Question: I'm trying to clean up some data and am struggling to do so in Python/Pandas.
I have a series of data with TV Show Titles. I would like to do the following:

  1. check if there are integers at the end of the string
  2. if there is only one integer, return everything before that part of the string
  3. if there are multiple parts of the string that are integers, return the first all of the string and then the 1st integer 

So here is my inputs:

    
    
    Brooklyn 99 103
    Hit The Floor 110
    

Outputs:

    
    
    Brooklyn 99
    Hit The Floor
    

As a separate function (or functions), I would like to remove any additional
season/ episode formatting and any strings after it :

Inputs

    
    
    Hot in Cleveland s6 ep03
    Mutt & Stuff #111
    LHH ATL 08/31a HD
    LHH ATL 04/04 Check
    Esther With Hot Chicks Ep. 1
    Suspect 2/24
    Suspect 2/24 HD
    

Output

    
    
    Hot in Cleveland
    Mutt & Stuff
    LHH ATL
    LHH ATL
    Esther With Hot Chicks
    Suspect
    Suspect
    

I've written a function like so:

    
    
    def digit(value):
        return value.isdigit()
    
    
    def another(value):
        li = value.split(" ")
        x = len(filter(digit, value))
        ind = li.index( str(filter(digit, li)[0]) ) 
        try: 
            if x > 1:
                return " ".join(li[:ind+1])
            else: 
                value.str.replace(r'(\D+).*', r'\1').str.replace(r'\s+.$', '').str.strip()
        except:
            return value.str.replace(r'(\D+).*', r'\1').str.replace(r'\s+.$', '').str.strip()
    
    data["LongTitleAdjusted"] = data["Long Title"].apply(another)
    data["LongTitleAdjusted"]
    

but I am getting this error:

    
    
    AttributeError                            Traceback (most recent call last)
    <ipython-input-49-3526b96a8f5a> in <module>()
         15         return value.str.replace(r'(\D+).*',      r'\1').str.replace(r'\s+.$', '').str.strip()
         16 
    --->     17 data["LongTitleAdjusted"] = data["Long Title"].apply(another)
             18 data["LongTitleAdjusted"]
    
    C:\Users\lehmank\AppData\Local\Continuum\Anaconda2\lib\site-     packages\pandas\core\series.pyc in apply(self, func, convert_dtype, args,   **kwds)
       2167             values = lib.map_infer(values, lib.Timestamp)
       2168 
    -> 2169         mapped = lib.map_infer(values, f, convert=convert_dtype)
       2170         if len(mapped) and isinstance(mapped[0], Series):
       2171             from pandas.core.frame import DataFrame
    
    pandas\src\inference.pyx in pandas.lib.map_infer (pandas\lib.c:62578)()
    
    <ipython-input-49-3526b96a8f5a> in another(value)
         13             value.str.replace(r'(\D+).*',    r'\1').str.replace(r'\s+.$', '').str.strip()
         14     except:
    ---> 15         return value.str.replace(r'(\D+).*',    r'\1').str.replace(r'\s+.$', '').str.strip()
         16 
          17 data["LongTitleAdjusted"] = data["Long Title"].apply(another)
    
    AttributeError: 'unicode' object has no attribute 'str'
    

for regex


Answer: this will do the trick with your sample data set:

    
    
    df['title'].str.replace(r'(\D+).*', r'\1').str.replace(r'\s+.$', '').str.strip()
    

but it would also convert `Brooklyn 99` to `Brooklyn`

