maxymoo maxymoo - 6 months ago 15
Python Question

How to do a complex query on a dataframe in a functional style

I like using

query
rather than
loc
since it is can be used with operator chaining, and can sometimes save characters by not repeating the
DataFrame
name.

import pandas as pd
dataframe_with_a_long_name = pd.DataFrame({'animals': ['cat', 'dog']})
dataframe_with_a_long_name.query("animals == 'cat'")


Is there a way to do a similar thing if I want to do is not supported by
query
like
str.contains
?

dataframe_with_a_long_name[dataframe_with_a_long_name.animals.str.contains('c')]

Answer

Since 0.18.1 .loc[], .iloc, .ix and also [] accept a callable so you can:

dataframe_with_a_long_name.loc[lambda df: df.animals.str.contains('c')]

  animals
0     cat
Comments