USING LIKE inside pandas.query()

后端 未结 6 588
庸人自扰
庸人自扰 2020-12-13 18:22

I have been using Pandas for more than 3 months and I have an fair idea about the dataframes accessing and querying etc.

I have got an requirement wherein I wanted t

6条回答
  •  一个人的身影
    2020-12-13 18:39

    A trick I just came up with for "starts with":

    pandas.query('"abc" <= column_name <= "abc~"')
    

    Explanation: pandas accepts "greater" and "less than" statements for strings in a query, so anything starting with "abc" will be greater or equal to "abc" in the lexicographic order. The tilde (~) is the largest character in the ASCII table, so anything starting with "abc" will be less than or equal to "abc~".

    A few things to take into consideration:

    • This is of course case sensitive. All lower case characters come after all upper cases characters in the ASCII table.
    • This won't work fully for Unicode strings, but the general principle should be the same.
    • I couldn't come up with parallel tricks for "contains" or "ends with".

提交回复
热议问题