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 select only rows from Pandas DataFrame with 3 characters in Python Pandas?

I have Pandas DataFrame like below, col1 is STRING data type:

col1
-----
"123"
"1111"
"287777"
NaN
"222"

And I need to select only these rows where string in "col1" has 3 characters, so as a result i need something like below:

col1:
-----
"123"
"222"

How can I do 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 pandas.Series.str.len function for this task:

df[df['col1'].str.len() == 3]

Output:

    col1
0   123
4   222
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