Search code examples
pythonpandasdataframesubstring

Check a column for substring to assign new columns with values


This is my dataframe with 2 columns:

ID      CODES
36233   LEH,PW
6175    N/A
6242    
6680    MS,XL,JFK

In column CODES, I need to identify the comma (",") and then count the number of commas and return it in a dataframe:

Output:

ID      CODES   HAS COMMA   NO. OF COMMAS
36233   LEH,PW  TRUE        1
6175    N/A     FALSE       0
6242            FALSE       0
6680  MS,XL,JFK TRUE        2

So far I've tried DF['HAS COMMA'] = np.where(DF['CODE'].str.contains(','),True, False) but this returns TRUE where there are blanks. :(

Additionally DF['NO OF COMMAs']=DF['CODE'].count(",") returns an error.


Solution

  • How about with:

    df['HAS COMMA'],df['NO. OF COMMA'] = [df.CODES.str.contains(',').fillna(False), df.CODES.str.count(',').fillna(0)]
    

    prints:

          ID      CODES  HAS COMMA  NO. OF COMMA
    0  36233     LEH,PW       True           1.0
    1   6175        N/A      False           0.0
    2   6242        NaN      False           0.0
    3   6680  MS,XL,JFK       True           2.0