Example # 01:
The “Spyder” tool is used to write every code script that we will discuss in this guide. Every time we want to develop new “pandas” code, we have to import a few “pandas” modules. These can be imported by simply following the term “import” with the phrase “pandas as pd. Now, that “pd” has been added to this code whenever we have to access the “pandas'” functions. Now, we need to create the DataFrame.
The “Raw_record” variable is where we add some data that we want to insert into the DataFrame. The dates are added first which are “12/08/22, 12/08/22, 13/08/22, 14/08/22, 15/08/22, and 17/08/22” in the “Dated”. Then, we have “Name” in which we put “Ball Pen, Pointer, Eraser, Stapler, Scale, and Glue Stick”. After this, we add the quantity of these items in the “Quantity” which are “18, 21, 36, 4, 15, and 9”. We also add the price in the “Price” column which is “350, 520, 220, 900, 90, and 250”.
Now, we are inserting the “pd.DataFrame()” which helps in converting this “Raw_record” into the “Raw_record_df”, which is the name of the DataFrame. We then initialize a variable named “values” with some data which are “Ball Pen, Pointer, Glue Stick”. After this, we are applying the “Not In” filter for checking some data. We utilize this “Not In” filter with the “isin()” method. For filtering data using the “Not In” method, we put the “~” sign. You can see below that we have mentioned the name of the DataFrame first and inside it, we place the column name in which we want to check the data.
We place the “~” sign before placing the column’s name with the DataFrame’s name. Then, we put the “isin()” method after this and pass the “values” variable to this “isin()” function. We store the result that we get after applying this function in the “Remaining_values” variable. Now, we place the “Remaining_values” in the “print()” so, the result we get here after applying this “Not In” filter will be displayed.
We may quickly obtain the results of the “pandas” codes in the “Spyder” by pressing “Shift+Enter” or the run icon. The outcome of this code contains the DataFrame with all the data that we added to it. Then, it filters some data and displays those rows in which the “Ball Pen, Pointer, and Glue Stick” are not present in the “Name” column. We filter this DataFrame with the help of the “Not In” filter method.
Example # 02:
The “Pre_rank_record” DataFrame is created in this example which contains “name, Pre_Rank, and New_Fee” columns. These columns also contain some data in them. In the “name” column, we have added “Stella, Tatum, George, Peter, Kenna, and Lila”. Then, we put “Python, DataBase, Artificial Intelligence, Python, Web development, and Python” in the “Pre_Rank” column. The fees we insert in the “Fee” column are: “1300, 1900, 2000, 1300, 1500, and 1300”. We also print the “Pre_rank_record” by using “print()”.
After completing this DataFrame, we put the “values1” variable and initialize it with two names which are “Stella, and George”. We then utilize the “Not In” filter for filtering the “name” columns in which the names are not in the values which we have added in the values1 variable. It means that the name is not “Stella” nor “George”. We place the name of the DataFrame, the “~” symbol, and the name of the DataFrame along with the column name in which we want to filter the data. The column where we have to apply this function is the “name” column and we also put the “values1” variable name in the “isin()” method.
Now, we also want to filter data from the “Pre_Rank” column. For this, we initialize the “values2” variable with some data from the “Pre_Rank” column that we want to filter from the “Pre_Rank” column. Now, we again place the “Not In” filter for filtering the data and displaying the remaining rows in which the “Python” is not present in the “Pre_Rank” column. After this, we also utilize the “Not In” filter for filtering the data from the last column which is “New_Fee”. We place the “values3” variable and initialize the “values3” with two values which we have added in the “New_Fee” column. Then, we print the remaining values in which the “values3” values are not present.
It displays the DataFrame, first, in this outcome. Then, it displays the DataFrame which we have entered in the code above. First, it displays those rows of the DataFramewhere “Stella and George” which are not present in the “name” column. Then, it displays those rows in which the “Python” is not present in the “Pre_Rank” column and also displays those rows of the DataFrame in which “1300, and 2000” are not present in the last “New_Fee” column.
Example # 03:
We utilize the “record” DataFrame which contains the same data that we have added in the “Pre_rank_record” DataFrame. We just changed the name of the DataFrame here. Now, we are filtering data by utilizing the “Not In” filter with multiple columns of the “Pre_rank_record” DataFrame. We put “Tatum, Kenna, 1900” in the “my_list1” variable where “Tatum, and Kenna” are the values of the “name” column and “1900” is the value of the “New_Fee” column.
As we have added the values of two different columns in the “my_list1” variable, we also insert the names of both columns below where we utilize the “Not In” filter. After adding the name of the DataFrame and the “~” symbol, we put the names of both columns with the name of the DataFrame. Then, we place the “isin()” method in which we pass the “my_list1” variable. We also place “axis=1” with this.
Now, we initialize another variable which is the “my_list2” variable with “Lila” and “Artificial Intelligence”. Here “Lila” is the value of the “name” column and “Artificial Intelligence” is the value of the “Pre_Rank” column. After this, we again utilize the “Not In” filter in the same way that we have explained in the lines above in this example.
After showing the complete DataFrame, it renders only those rows in which the rows don’t contain “Tatum, Kenna, and 1900” in the “name” and “New_Fee” columns. Then, it renders the rows where we don’t have “Lila and Artificial Intelligence” in the “name” and “Pre_rank” columns.
Conclusion
The “Not In” filter is explained in this guide in detail along with the codes and outcomes of that codes. We have explained how to filter the data using the “Not In” method and what is the best way of using this method in “pandas”. We have also shown the codes in which we have utilized this “Not In” filter with the “isin()” method and have explained each step of the code in detail here. We have rendered the data of the DataFrame after applying the “Not In” filter method to the DataFrame and have shown the DataFrame after applying the “Not In” filter and have explained that it displays the remaining data of the DataFrame in which the values are not present which we have mentioned in the “Not In” filter method.