Home > Net >  How to select only rows from Pandas DataFrame with 3 characters in Python Pandas?
How to select only rows from Pandas DataFrame with 3 characters in Python Pandas?

Time:07-05

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?

CodePudding user response:

You can use the pandas.Series.str.len function for this task:

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

Output:

    col1
0   123
4   222
  • Related