I have a function called ValidString(s)
that tests a string and returns whether it's valid or not in boolean True/False. I need to use this function on a specific column that contains strings, and based on the results of true/false, populate 'good'
or 'no good'
.
I tried below, but it's returning either:
'float' object is not subscriptable
or
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
df["good"] = df['string'].apply(ValidString)
Edit: function checks string if it complies with format "A5b2c3" where number after b and c need to equal to # after A. Abc are set text. I'm not using any libraries. For example:
isValid = False
if s == "" or s[0] != "A" or len(s)<=5 or s[1] == "0" or "b" not in s or "c" not in s:
return isValid
I know the function does work on the string. The new dataframe has a column with values exactly the same format - "A5b2c3".
I think the setup is OK, I'm struggling with basic python syntax somewhere and have been bashing my heads for hours. Is it somehow because the original function is ValidString(s) --with (s)? And I'm trying to use .apply without that?
Maybe I don't need to add it as a new column directly, can I store the booleans of the test in another dataframe, then combine them afterwards using if statements? In other words.. how do I use this function, ValidString(s), to test a column instead of a single value (s)?