2938

@2938

Stats, Money, and NYC

Guestbook

pandas.DataFrame.query

The shittiest dataframe method.

There's (at least) two common ways to filter a dataframe by rows that have a certain value in a column.

a = df.query('name == "myname"')
b = df[df['name'] == 'myname']

pandas.DataFrame.query is really bizarre (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.query.html). It takes a string as a parameter and that string is evaluated with pandas.eval(). The string can use @ to refer to variables.

It's still better than the second method. You can nicely chain the query method, which can't really be said for the second method.

(df
 .query('name == "myname"')
 .groupby('city')
 .sum()
 .reset_index()
 .head())

You'll only receive email when 2938 publishes a new post

More from 2938: