NumPy | Python Methods and Functions

** **

Pandas ** Series.combine() ** — it is a series mathematical operation method. This is used to combine two series into one. The shape of the output series is the same as that of the caller series. The elements are defined by the function passed as a parameter to the

` combine () `

method. The shape of both series must be the same, otherwise it will lead to an error.

Syntax:Series.combine (other, func, fill_value = nan)

Parameters:

other:other series or list type to be combined with caller series

func:Function passed as parameter which will decide from which series the element should be put at that index

fill_value:integer value of level in case of multi index

Return: Combined series with same shape as caller series

** Example # 1: **

In this example, two lists are created and converted into a series pandas using the .Series () method. The function is created using a lambda that checks which values are lower in both series and returns which is lower.

` ` |

** Output: **

As shown in the output image, the returned row has lower values from both rows.

** Example # 2: **

In this example, NULL values are also passed from the ` Numpy.nan `

method of the ` Numpy.nan `

method. Since the series contains zero values, 5 is passed to the fill_value parameter to replace the zero values with 5. A lambda function is passed that compares the values in both series and returns a larger value.

` `

` ` ` # pandas module import `

` import `

` pandas as pd `

` # numpy module import `

` import `

` numpy as np `

` # create the first episode `

` first `

` = `

` [`

` 1 `

`, `

` 2 `

`, np.nan, `

` 5 `

`, `

` 6 `

`, `

` 3 `

`, np.nan, `

` 7 `

`, `

` 11 `

`, `

` 0 `

`, `

` 4 `

`, `

` 8 `

`] `

` # create the second episode `

` second `

` = `

` [`

` 5 `

`, `

` 3 `

`, `

` 2 `

`, np.nan, `

` 1 `

`, `

` 3 `

`, `

` 9 `

`, `

` 21 `

`, `

` 3 `

`, np.nan, `` 1 `

`, np.nan] `

` ` ` `

` # create series `

` ` ` first `

` = `

` pd.Series (first) `

` # create the series `

` second `

` = `

` pd.Series (second) `

` # method call .combine () `

` result `

` = `

` first.combine (second , func `

` = `

` (`

` lambda `

` x1, x2: x1 `

` if `

` x1 & gt; x2 `

` else `

` x2), fill_value `

` = `

` 5 `

`) `

` # display `

` result `

` `

** Output: **

As shown in the output, the NaN values in the series were replaced by 5 before the series was merged.

0 5.0 1 3.0 2 2.0 3 5.0 4 6.0 5 3.0 6 9.0 7 21.0 8 11.0 9 5.0 10 4.0 11 8.0 dtype: float64

For many decades, some powerful trends have been in place. Computer hardware has rap- idly been getting faster, cheaper and smaller. Internet bandwidth (that is, its information carrying capacity) has...

23/09/2020

Pandas 1.x Cookbook: Practical recipes for scientific computing, time series analysis, and exploratory data analysis using Python, 2nd Edition....

05/09/2021

Target knows. Apple Computer knows, too. So do LinkedIn, Netflix, Facebook, Twitter, Expedia, national and local political campaigns, and dozens of other organizations that all generate enormous eco...

10/07/2020

Python Crash Course is the world's best-selling guide to the Python programming language. This quick and in-depth introduction to Python programming will get you started writing programs, solving prob...

11/08/2021

X
# Submit new EBook