How can I merge two or more series in Pandas?

How can I merge two or more series in Pandas?

Pandas is a popular Python library used for data manipulation and analysis. It provides a powerful tool called “Series” for working with one-dimensional data. However, there may be situations where we need to combine or merge two or more series together to perform advanced analysis. This can be achieved using the “concat” or “append” functions in Pandas. These functions allow us to combine multiple series into a single series, or add new data to an existing series. By merging series in Pandas, we can efficiently manage and analyze large datasets with ease.

Merge Two or More Series in Pandas (With Examples)


You can use the following syntax to quickly merge two or more series together into a single pandas DataFrame:

df = pd.concat([series1, series2, ...], axis=1)

The following examples show how to use this syntax in practice.

Example 1: Merge Two Series in Pandas

The following code shows how to merge together two pandas Series into a single pandas DataFrame:

import pandas as pd

#define series
series1 = pd.Series(['Mavs', 'Rockets', 'Spurs'], name='Team')
series2 = pd.Series([109, 103, 98], name='Points')

#merge series into DataFrame
df = pd.concat([series1, series2], axis=1)

#view DataFrame
df

        Team	Points
0	Mavs	109
1	Rockets	103
2	Spurs	98

Note that if one series is longer than the other, pandas will automatically provide NaN values for missing values in the resulting DataFrame:

import pandas as pd

#define series
series1 = pd.Series(['Mavs', 'Rockets', 'Spurs'], name='Team')
series2 = pd.Series([109, 103], name='Points')

#merge series into DataFrame
df = pd.concat([series1, series2], axis=1)

#view DataFrame
df

        Team	Points
0	Mavs	109
1	Rockets	103
2	Spurs	NaN

Example 2: Merge Multiple Series in Pandas

The following code shows how to merge multiple series into a single pandas DataFrame:

import pandas as pd

#define series
series1 = pd.Series(['Mavs', 'Rockets', 'Spurs'], name='Team')
series2 = pd.Series([109, 103, 98], name='Points')
series3 = pd.Series([22, 18, 15], name='Assists')
series4 = pd.Series([30, 35, 28], name='Rebounds')

#merge series into DataFrame
df = pd.concat([series1, series2, series3, series4], axis=1)

#view DataFrame
df

	Team	Points	Assists	Rebounds
0	Mavs	109	22	30
1	Rockets	103	18	35
2	Spurs	98	15	28

Cite this article

stats writer (2024). How can I merge two or more series in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-merge-two-or-more-series-in-pandas/

stats writer. "How can I merge two or more series in Pandas?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-merge-two-or-more-series-in-pandas/.

stats writer. "How can I merge two or more series in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-merge-two-or-more-series-in-pandas/.

stats writer (2024) 'How can I merge two or more series in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-merge-two-or-more-series-in-pandas/.

[1] stats writer, "How can I merge two or more series in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I merge two or more series in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top