![]() |
VOOZH | about |
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.replace() function is used to replace values given in to_replace with value. The values of the Series are replaced with other values dynamically.
Series.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='None')
Parameters:
Returns: Object after replacement.
Example 1: Use Series.replace() function to replace some values from the given Series object.
Output :
Coca Cola 10 Sprite 25 Coke 3 Fanta 11 Dew 24 ThumbsUp 6 dtype: int64
Now we will use Series.replace() function to replace the old values with the new ones.
Output :
Coca Cola 10 Sprite 25 Coke 1000 Fanta 11 Dew 24 ThumbsUp 6 dtype: int64
As we can see in the output, the Series.replace() function has successfully replaced the old value with the new one.
Example 2 : Use Series.replace() function to replace some values from the given Series object.
Output :
City 1 New York City 2 Chicago City 3 Toronto City 4 Lisbon City 5 Rio dtype: object
Now we will use Series.replace() function to replace the old values with the new ones using a list.
Output :
City 1 London City 2 Chicago City 3 Toronto City 4 Lisbon City 5 Brisbane dtype: object
As we can see in the output, the Series.replace() function has successfully replaced the old value with the new one using the list.