Is there a way to use a methods for Boolean indexing in pandas DataFrame?
For example:
import pandas
def filter_func(v) -> bool:
return v == 'asd'
def main():
df_test = pandas.DataFrame(
[
['sd'], ['asd'], ['sdf']
],
columns=["col-a"]
)
#### ERROR: This next line calls filter_func with all contents of column 'col-a'
result = df_test[df_test['col-a'] == filter_func(df_test['col-a'])]
if __name__ == '__main__':
main()
In the example above I want to keep only those values for which filter_func will return True. And a result should contain dataframe with single row, but instead I'm getting empty dataframe.
I understand that instead of executing filter_func for each row it is executed only once.
Is there a way to call it for each row?
Should I use apply or map for Series in this case?
Or is there any other way?