How do I can filter pandas DataFrame by slice of column value

codkelden

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

LTheriault

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I slice this dataframe?

How do I filter a pandas DataFrame based on value counts?

pandas, how to filter dataframe by column value

How do i slice pandas Series with DatetimeIndex and put it in a DataFrame by rows?

How do I take max value of a pandas dataframe column and find the corresponding value in another column?

How do I use python or pandas to filter a DataFrame based on a column that consists of a list of dictionary?

How do I update the values in a pandas dataframe column until first occurance of a value in the same column?

How can I assign a color to a specific value in a column of a pandas dataframe?

Why can't I filter Pandas dataframe on numeric column

How can I populate a pandas dataframe column with tests on the value of another column?

How can I compare a column in a data frame by a value in a column with the same name/place in a second dataframe in Pandas?

How can I iterate through a column of a pandas DataFrame and return value from another column?

How can I assign a new column to a slice of a pandas DataFrame with a multiindex?

In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

How do I replace a slice of a dataframe column with values from another dataframe column slice?

How do I find the index of a known value in a pandas dataframe column?

How can I slice elements of one Pandas dataframe column by different values?

How do I slice a pandas dataframe based on a condition?

How can I slice the beginning and end of a pandas dataframe?

Using Pandas in Python 3, how do I filter out repeat strings in a column within a dataframe?

How can I filter spark Dataframe according to the value that column contains?

How can I filter a pandas dataframe of substrings based on another dataframe's column of full strings?

In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column?

How do I filter a dataframe by a datetime64 column in pandas using minutes and seconds?

How do I use the length of another column in Pandas as a slice argument

How to split/slice a Pandas dataframe into multiple dataframes by column value?

How do I append to a Pandas DataFrame column?

How can I filter a Pandas DataFrame based on whether all aggregated values in a column are True?

How can I create a new column in a pandas dataframe that counts backwards the rows based on the value of another column?