I would like to return a filtered dataframe that does NOT contain a specific integer, like 2. However, it does need to return rows that have integers like 12, or 22, or 200...etc.
Example:
d = {'num_list': ["1,2,3,10,11,12,13","","4,5,6","11,12,13","2,3,4,12","12,13"]}
searchfor = "2"
df = pd.DataFrame(data=d)
filtered_df = df[~df['num_list'].str.contains(searchfor)]
The dataframe:
num_list
0 1,2,3,10,11,12,13
1
2 4,5,6
3 11,12,13
4 2,3,4,12
5 12,13
Expected result:
num_list
1
2 4,5,6
3 11,12,13
5 12,13
Actual result:
num_list
1
2 4,5,6
This code is matching the string "2" which also exists in row 3 and 5. Trying to find the right method to solve this. I'm thinking of changing colum num_list to a list, but I don't know how to filter a dataframe list.
d = {'num_list': [[1,2,3,10,11,12,13],[],[4,5,6],[11,12,13],[2,3,4,12],[12,13]]}
searchfor = 2
df = pd.DataFrome(data=d)
??
The dataframe:
num_list
0 [1, 2, 3, 10, 11, 12, 13]
1 []
2 [4, 5, 6]
3 [11, 12, 13]
4 [2, 3, 4, 12]
5 [12, 13]
Is this the right approach? How do I return rows that does not have the specific integer 2 (i.e. return row 1,2,3,5)? Thanks in advance.