Suppose I have a following Dataframe:
ter_id shstr value
6 2018002000000 201 1740.0
7 2018002000000 201 10759.0
8 2018002000002 201 2.0
How do I can filter out rows with last six symbols of ter_id
is zeroes? That is desired output is:
ter_id shstr value
8 2018002000002 201 2.0
I made a boolean function
def is_total(ter_id: str) -> bool:
if ter_id[:-6] == "000000":
return True
return False
But it usage fail with error:
dataset.filter(is_total(dataset.ter_id))
...
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Pandas version is 1.0.1
For filtering a dataframe based on column values, there is rarely a reason to write your own function. You can pass the conditions as a boolean mask into df.loc[] (assuming your DataFrame is named df).
df = df.loc[df["ter_id"].str[-6:] != "000000"]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments