TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas DataFrame.isin()

Pandas DataFrame.isin() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

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.

Syntax

DataFrame.isin(values)

Parameter

values : 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[]




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf