Pandas isin() method helps search the input set of values in the given DataFrame . We will discuss Pandas, its isin() method, and its examples.
What is Pandas in Python?
Pandas is Python’s standard data frame module. You should almost likely use Pandas if you’re working with tabular data in Python.
It gives a very efficient data structure and tools for performing data analysis. Pandas is a Python module for data science and analytics that runs on top of NumPy. The DataFrame in Pandas’ fundamental data structure allows us to store and alter tabular data in a 2-D structure.
What is DataFrame?
The most essential and extensively used data structure is the DataFrame, a standard way to store data. DataFrame has data organized in rows and columns like an SQL table or a spreadsheet database. We may either convert our custom data into a DataFrame or import data from a CSV, tsv, Excel, SQL database, or from another source.
What is Pandas Isin() Function?
The isin() function checks if the provided value(s) are present in the Dataframe. This function returns a boolean DataFrame. The DataFrame appears to be the same as the original and is unaltered. Still, the original values are replaced with True if the data frame element is one of the specified elements, and is changed to False otherwise.
Examples of Isin() Method
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 | import pandas as pd data = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Roll number': [25, 40, 23, 22], 'Height': ['169', '173', '173', '178'] }) heights_to_filter = ['173', '169', '177'] result = data.isin(heights_to_filter) print(result) |
Output:
Name | Roll | number | Height |
---|---|---|---|
0 | False | False | True |
1 | False | False | True |
2 | False | False | True |
3 | False | False | False |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import pandas as pd data = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Age': [25, 45, 23, 32], 'Favorite Subject': ['Math', 'Science', 'Science', 'English'] }) dict_data_to_filter = {'Name': ['B', 'D'], 'Department': ['Science']} result = data.isin(dict_data_to_filter) print(result) |
Output:
Name | Age | Favorite | Subject |
---|---|---|---|
0 | False | False | False |
1 | True | False | False |
2 | False | False | False |
3 | True | False | False |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 | import pandas as pd data = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Age': [25, 45, 23, 32], 'Department': ['29', '35', '35', '40'] }) series_data = pd.Series(['A', 'C', 'B', 'D']) result = data.isin(series_data) print(result) |
Output:
Name | Age | Department |
---|---|---|
0 | True | False |
1 | False | False |
2 | False | False |
3 | True | False |
Example 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import pandas as pd data = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Roll number': [25, 45, 23, 32], 'House': ['Blue', 'Green', 'Green', 'Yellow'] }) df = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Roll number': [25, 45, 23, 32], 'House': ['Blue', 'Green', 'Green', 'Yellow'] }) result = data.isin(df) print(result) print() df = pd.DataFrame({ 'Name': ['A', 'B', 'C', 'D'], 'Roll number': [25, 45, 23, 32], 'House': ['Blue', 'Green', 'Green', 'Yellow'] }) result = data.isin(df) print(result) |
Output:
Name | Roll | number | House |
---|---|---|---|
0 | True | True | True |
1 | True | True | True |
2 | True | True | True |
3 | True | True | True |
Name | Roll | number | House |
---|---|---|---|
0 | True | True | True |
1 | True | True | True |
2 | True | True | True |
3 | True | True | True |
Conclusion
We discussed Pandas in Python, the DataFrame, the Pandas isin() function, and some isin() method examples. The isin() method is used to get the boolean DataFrame that tells which input values are present in the DataFrame.