C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DataFrame.isin()The main task of the DataFrame.isin() method is to select the rows having a particular (or multiple) values in a particular column. SyntaxDataFrame.isin(values) Parametervalues : It can be DataFrame, Series, Iterable, or dict and returns a boolean value. It returns a true value if all the labels match. If it consists of a Series, then it will be the index. If it consists of a dict, then the keys must be the column names and must be matched. If it consists of a DataFrame, then both the index and column labels must be matched. Example1:
import pandas as pd
#initializing dataframe
info = pd.DataFrame({'x': [1, 2], 'y': [3, 7]})
#check if the values of info are in the range(1,6)
p = info.isin(range(1,8))
print('DataFrame\n-----------\n',info)
print('\nDataFrame.isin(range(1,6))\n-----------\n',p)
Output: DataFrame ----------- xy 0 1 3 1 2 7 DataFrame.isin(range(1,6)) ----------- xy 0 TrueTrue 1 TrueTrue Example2:
import pandas as pd
data = pd.DataFrame({
'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'],
'Name': ['Parker', 'Smith', 'Jones', 'Terry', 'Palin'],
'Occupation': ['Tester', 'Developer', 'Statistician',
'Tester', 'Developer'],
'Date Of Join': ['2019-01-17', '2019-01-26', '2019-01-29', '2019-02-02',
'2019-02-11'],
'Age': [29, 22, 25, 38, 27]})
print("\nUseisin operator\n")
print(data.loc[data['Occupation'].isin(['Tester','Developer'])])
print("\nMultiple Conditions\n")
print(data.loc[(data['Occupation'] == 'Tester') |
(data['Name'] == 'John') &
(data['Age'] < 27)])
Output: Use isin operator EmpCodeNameOccupation Date Of Join Age 0 Emp001 Parker Tester 2019-01-17 29 1 Emp002 Smith Developer 2019-01-26 22 3 Emp004 Terry Tester 2019-02-02 38 4 Emp005 Palin Developer 2019-02-11 27 Multiple Conditions EmpCode Name Occupation Date Of Join Age 0 Emp001 Parker Tester 2019-01-17 29 3 Emp004 Terry Tester 2019-02-02 38
Next TopicDataFrame.loc[]
|