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)
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
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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-update.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics