Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to verify whether string value is in correct range in Data Frame in Python Pandas?

I have DataFrame in Python pandas like below ("col1" is data type string):

col1
-------
14212614414
05261265140
82044114467
...
  • Fifth and sixth values mean day of born.

How to check whether day are from the correct range, so fifth and sixth values should be from range 1 to 31.

col1        | col2
------------|-------
14212614414 | True
05261265140 | True
82044114467 | False
...

How can Ido that in Python Pandas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the series.str accessor and slice the string, then treat it as an int with astype and check if it’s between the numbers

df['col2']=df['col1'].str[4:6].astype(int).between(1,31)


col1    col2
0   14212614414 True
1   5261265140  True
2   82044114467 False
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading