w3resource

Pandas Series: update() function

Modify Pandas series in place using non-NA values

The update() function is used to modify series in place using non-NA values from passed Series.

Aligns on index.

Syntax:

Series.update(self, other)
Pandas Series update image

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4])
s.update(pd.Series([5, 6, 7]))
s

Output:

0    5
1    6
2    7
dtype: int64
Pandas Series update image

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['p', 'q', 'r'])
s.update(pd.Series(['s', 't'], index=[0, 2]))
s

Output:

0    s
1    q
2    t
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4])
s.update(pd.Series([5, 6, 7, 8, 9]))
s

Output:

0    5
1    6
2    7
dtype: int64

Example - If other contains NaNs the corresponding values are not updated in the original Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([1, 2, 3])
s.update(pd.Series([6, np.nan, 8]))
s

Output:

0    6
1    2
2    8
dtype: int64

Previous: Replace Pandas series values given in to_replace with value
Next: Convert Pandas TimeSeries to specified frequency



Follow us on Facebook and Twitter for latest update.