TheDeveloperBlog.com

Home | Contact Us

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

Pandas DataFrame.where()

Pandas DataFrame.where() 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.where()

The main task of the where() method is to check the data frame for one or more conditions and return the result accordingly. By default, if the rows are not satisfying the condition, it is filled with NaN value.

Syntax

DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False, raise_on_error=None)

Parameters

  • cond: It refers to one or more conditions to check the data frame.
  • other: It replaces the rows that do not satisfy the condition with the user-defined object; the default value is NaN.
  • inplace: Returns the boolean value. If the value is true, it makes the changes in the dataframe itself.
  • axis: An axis to check( row or columns).

Returns

Example1

import pandas as pd
import numpy as np
a = pd.Series(range(5))
a.where(a > 0)
a.mask(a > 0) 
a.where(a > 1, 10)
info = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B'])
info
b = info % 3 == 0
info.where(b, -info)
info.where(b, -info) == np.where(b, info, -info)
info.where(b, -info) == info.mask(~b, -info)

Output

     A        B
0   True     True
1   True     True
2   True     True
3   True     True
4   True     True





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