Can someone help to validate a column. It's about a 'Initials' column.
Contact | Initials |
---|---|
1 | P.J. |
2 | Peter |
3 | P. |
An Initial exist of one letter and then a point. So like rows one and three. Row 2 is false.
I hope someone can help.
Can someone help to validate a column. It's about a 'Initials' column.
Contact | Initials |
---|---|
1 | P.J. |
2 | Peter |
3 | P. |
An Initial exist of one letter and then a point. So like rows one and three. Row 2 is false.
I hope someone can help.
Use Series.str.contains
for test uppercase with dot:
print (df)
Contact Initials
0 1 P.J.
1 2 Peter
2 3 P.Daa.
3 4 P.
4 5 H..
5 6 J.K
#https://stackoverflow.com/a/17779796/2901002 with ^ for start and $ for end of string
df['test'] = df['Initials'].str.contains(r'^(?:[A-Z]\.)+$')
print (df)
Contact Initials test
0 1 P.J. True
1 2 Peter False
2 3 P.Daa. False
3 4 P. True
4 5 H.. False
5 6 J.K False
You could consider writing a custom function to check for your two conditions:
def validate(string):
if not string[0].isalpha():
return False
if not string[1] == ".":
return False
return True
Then apply it to the columns like so:
>>> df["Initials"].apply(validate)
0 True
1 False
2 True