+

Python | Pandas Series.update ()

Pandas Series — it is a one-dimensional array with axis labels. Tags do not have to be unique, but must be hashable. The object supports indexing based on integers and labels and provides many methods for performing index-related operations.

Series.update() Pandas Series.update () modifies the Series in place using the non-NA values ​​from the passed Series object. The function is indexed.

Syntax: Series.update (other)

Parameter:
other: series

Returns: None

Example # 1: Use Series.update () to update the values ​​of some cities in this Series object

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ `New York` , `Chicago` , None , < / code> `Toronto` , ` Lisbon` , `Rio` , ` Chicago` , `Lisbon` ])

 
# Print series

print (sr)

Output:

Now we will use Series.update () to update the values ​​identified by the passed indexed in this Series object.

# update values ​​at the passed index
# from the values ​​in the passed object of the series

sr.update (pd.Series ([ `Melbourne` , ` Moscow` ], index = [ 2 , 7 ]))

Output:

As we can see from the output, Series.update () has successfully updated the values ​​in the original series object from the passed series object.

Example # 2: Use Series.update () to update the values ​​of some elements in this Series object

  # import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ 100 , 214 , 325 , 88 , None , 325 , None , 325 , 100 ])

 
# Print episode

print (sr)

Output:

Now we we will use Series.update () to update the values ​​identified by the passed indexed in the given Series object.

< / table>

Output:

As we can see from the output, Series.update () has successfully updated the values ​​in the original series object from the passed series object.

Get Solution for free from DataCamp guru

# update values ​​at the passed index
# from the values ​​in the passed series object

sr.update (pd.Series ( [ 5000 , 6000 ], index = [ 4 , 6 ]))