[Python] Pandas filter string data based on its string length using DataFrame.query

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    The question is very similar to this question Python: Pandas filter string data based on its string length, but I want to use pandas.DataFrame.query. Let's say we have a pandas.DataFrame. I like to filter out the rows where the string length of the column A is not equal to 3 using pandas.DataFrame.query

    import pandas as pd
    import numpy as np
    df = pd.DataFrame({'A' : ['hi', 'hello', 'day', np.nan], 'B' : [1, 2, 3, 4]})
    df.query('A.str.len() != 3')


    However, I got the following error

    TypeError: unhashable type: 'numpy.ndarray'

    Continue reading...

Compartilhe esta Página