Heisenberg Heisenberg - 1 month ago 8
Python Question

How to look up rows in certain condition

I have a dataframe and I would like to search strange rows like below :

  month
0 201605    
1 201606
2 201607
3 08
4 nan
5 201610


For instance, I would like to extract rows with elements that is not 6 digits like below :

month
3 08
4 nan


I have searched, but couldn't figure out how to extract rows.
How can I get this result?

Answer

Suppose your month column is of str type, you can use .str.len() to get the number of digits for each element and use the result for subsetting:

df[df.month.str.len() != 6]

# month
#3   08
#4  NaN
Comments