'How to filter a column by the last characters in Python
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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
