Search code examples
pythonpandasdataframegroup-byseries

Convert pandas series strings to numbers


`Following series, contains result as string of lists with values either PASS or FAIL. Input:-

result
"['PASS','FAIL']"
"['PASS','FAIL','PASS','FAIL']"
"['FAIL','FAIL']"

Output:

result
1
1
0

If any row has at-least one PASS as value then return 1 else return 0 Input:-

result
"['PASS','FAIL']"
"['PASS','FAIL','PASS','FAIL']"
"['FAIL','FAIL']"

Solution

  • If there are lists use in statement:

    df['result'] = [int('PASS' in x) for x in df['result']]
    #alternative solution
    df['result'] = df['result'].apply(lambda x: 'PASS' in x).astype(int)
    

    If strings use Series.str.contains:

    df['result'] = df['result'].str.contains('PASS').astype(int)