Convert pandas series strings to numbers
Question:
`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’]"
Answers:
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)
A simple and fast approach, use a regex with str.contains
:
# if your want a robust check
df['result'] = df['result'].str.contains(r'bPASSb').astype(int)
# or if you're sure there are only PASS/FAIL
df['result'] = df['result'].str.contains('PASS').astype(int)
`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’]" |
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)
A simple and fast approach, use a regex with str.contains
:
# if your want a robust check
df['result'] = df['result'].str.contains(r'bPASSb').astype(int)
# or if you're sure there are only PASS/FAIL
df['result'] = df['result'].str.contains('PASS').astype(int)