Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas query function with like and date

I have a python function that get a pandas dataframe and perform on it a condiotion by the function "query" of DataFrame.

So simple conditions like =, != and all that it's ok.

But I want it to perform a conditions with "like". Is that possible?

Also, I want to check if some column is bigger than "now"...

How could I do that ?

like image 813
user2671057 Avatar asked Aug 17 '17 11:08

user2671057


2 Answers

Demo:

In [166]: now = pd.datetime.today()

In [167]: df
Out[167]:
        date              s
0 2017-01-18  sample string
1 2017-12-01      blah-blah
2 2017-08-17          a key

In [168]: df.query("s.str.contains('key')", engine='python')
Out[168]:
        date      s
2 2017-08-17  a key

In [169]: df.query("s.str.contains('key') or date > @now", engine='python')
Out[169]:
        date          s
1 2017-12-01  blah-blah
2 2017-08-17      a key
like image 194
MaxU - stop WAR against UA Avatar answered Sep 18 '22 08:09

MaxU - stop WAR against UA


I think LIKE functionality is not implemented to query native way, need contains, match and similar functions.

For compare with today:

now = pd.datetime.today()

df.query("col > @now")
like image 40
jezrael Avatar answered Sep 20 '22 08:09

jezrael