Search code examples
pythonpandasfilterdigits

How to get/check number of digits of a row in a df.query()?


I have a dataframe df like:

number | col1
123    | a
1234   | b
567    | c

I want to count how many rows have more or less than 3 digits in column "number". The column as dtype = int64.

When I try:

count = df.query(
    """
    ... 'some other filters' or \
    number.str.len() != 3
    """
)
print(count)

I get:

AttributeError: Can only use .str accessor with string values!

Trying to convert to str() with

count = df.query(
    """
    ... 'some other filters' or \
    str(number).str.len() != 3
    """
)
print(count)

ValueError: "str" is not a supported function

Converting to string would also count negative signs so that's not really a solution anyway.

Removing that check for len = 3 removes all error messages and prints the count.

How can I count the digits in a dataframe query without converting to str and without apply (way too slow for my amount of rows)?


Solution

  • In [25]: df = pd.DataFrame({'number':[123, 25, 1234, -165, -99, 567], 'col' : ['a','b','c', 'd', 'e', 'f']})
    
    In [26]: df
    Out[26]:
       number col
    0     123   a
    1      25   b
    2    1234   c
    3    -165   d
    4     -99   e
    5     567   f
    
    In [27]: df.query("100 <= abs(number) <= 999")['number'].size
    Out[27]: 3