Reputation: 23
I have one data set and one of the columns has values such as: 001, 002, XXX, R02, T01, 003, 004. I need to drop all rows from the data set where the values of that column have at least 1 letter in the format (R02, T01, XXX) and leave just the number formats. In other words, I need to keep all the data where that column has the following values: 001, 002, 003, 004.
I have tried to use the following function, but I had no luck.
searchfor = ['r', 't']
Filtered_Table = Filtered_Table[~Filtered_Table.ToBeDeleted2.str.contains('|'.join(searchfor))]
Upvotes: 1
Views: 55
Reputation: 12684
Python is case sensitive so you need to use below to search ['R', 'T']
searchfor = ['R', 'T'] Filtered_Table = Filtered_Table[~Filtered_Table.ToBeDeleted2.str.contains('|'.join(searchfor))]
Demo:
import pandas as pd
Filtered_Table = pd.DataFrame([['001', '002'], ['R02', 'XXX'], ['T01', '003'], ['007', '008']], columns=["ToBeDeleted2", "ColB"])
searchfor = ['R', 'T']
Filtered_Table = Filtered_Table[~Filtered_Table.ToBeDeleted2.str.contains('|'.join(searchfor))]
Input:
ToBeDeleted2 ColB
0 001 002
1 R02 XXX
2 T01 003
3 007 008
Output:
ToBeDeleted2 ColB
0 001 002
3 007 008
Upvotes: 1
Reputation: 647
You could probably do what you are trying to do with pandas.
Using df.drop()
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html
https://chrisalbon.com/python/data_wrangling/pandas_dropping_column_and_rows/
Those are two helpful links for how the function works.
You can also try using the Regex re.search method.
https://www.w3schools.com/python/python_regex.asp
Upvotes: 2