Home > Software engineering >  Convert pandas series strings to numbers
Convert pandas series strings to numbers

Time:12-23

`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']"

CodePudding user response:

A simple and fast approach, use a regex with str.contains:

# if your want a robust check
df['result'] = df['result'].str.contains(r'\bPASS\b').astype(int)

# or if you're sure there are only PASS/FAIL
df['result'] = df['result'].str.contains('PASS').astype(int)

CodePudding user response:

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)
  • Related