Heisenberg Heisenberg - 1 year ago 58
Python Question

How to look up rows in certain condition

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

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 :

3 08
4 nan

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download