Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a column by the last characters in Python

Tags:

python

pandas

I have a column with a string similar to

country string_num
Botswana 864-0-0
Germany 968-0-5
Thailand 684-1-0

I would like to filter out all the strings that end with the numbers-0-0 and get a full data set view of rest.

I have tried the following code:

new_df = df[df['string_num'] > '-0-0']

The code runs, but I still see the rows that end with -0-0.

What can I change on my code that will have me seeing only the string_num that are greater than -0-0?

Thank you in advance for the assistance.

like image 283
Mbalenhle Avatar asked Oct 22 '25 10:10

Mbalenhle


1 Answers

You can use str.endswith and invert the boolean Series with ~:

new_df = df[~df['string_num'].str.endswith('-0-0')]

or using a regex ($ anchors the pattern to the end of the string):

new_df = df[~df['string_num'].str.contains('-0-0$')]

output:

    country string_num
1   Germany    968-0-5
2  Thailand    684-1-0
like image 74
mozway Avatar answered Oct 25 '25 01:10

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!