Python | Pandas Series.where - GeeksforGeeks

28 Jan, 2019
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.where() function replace values where the input condition is False for the given Series object. It takes another object as an input which will be used to replace the value from the original object.
Syntax: Series.where(cond, other=nan, inplace=False, axis=None, level=None, errors=’raise’, try_cast=False, raise_on_error=None)
Parameters :cond : boolean NDFrame, array-like, or callableother : scalar, NDFrame, or callableinplace : boolean, default Falseaxis : int, default Nonelevel : int, default Noneerrors : str, {‘raise’, ‘ignore’}, default raisetry_cast : boolean, default False
Returns : wh : same type as caller
Example #1: Use Series.where() function to replace values in the given Series object with some other value when the passed condition is not satisfied.
# importing pandas as pdimport pandas as pd  # Creating the First Seriessr1 = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])  # Creating the row axis labelssr1.index = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']   # Print the seriesprint(sr1)  # Creating the second Seriessr2 = pd.Series(['New York', 'Bangkok', 'London', 'Lisbon', 'Brisbane'])  # Creating the row axis labelssr2.index = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']  # Print the seriesprint(sr2)
Output :Now we will use Series.where() function to replace those values which does not satisfy the passed condition.
# replace the valuessr1.where(sr1 == 'Rio', sr2)
Output :As we can see in the output, the Series.where() function has replaced the names of all cities except the ‘Rio’ city. Example #2 : Use Series.where() function to replace values in the given Series object with some other value when the passed condition is not satisfied.
# importing pandas as pdimport pandas as pd  # Creating the First Seriessr1 = pd.Series([22, 18, 19, 20, 21])  # Creating the row axis labelssr1.index = ['Student 1', 'Student 2', 'Student 3', 'Student 4', 'Student 5']  # Print the seriesprint(sr1)  # Creating the second Seriessr2 = pd.Series([19, 16, 22, 20, 18])  # Creating the row axis labelssr2.index = ['Student 1', 'Student 2', 'Student 3', 'Student 4', 'Student 5']  # Print the seriesprint(sr2)
Output :
Now we will use Series.where() function to replace those values which does not satisfy the passed condition.
# replace the valuessr1.where(sr1 >20, sr2)
Output :As we can see in the output, the Series.where() function has replaced all the values which did not satisfy the passed condition.
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()