Concatenating two pandas series

Overview:

  • The method append(), concatenates one Series with another Series to produce a new Series.
  • The way two pandas series are attached using append() is a straight concatenation - one after another.
  • If elements from two series have to be combined using a specific semantics, like the elements need to be compared and added into the new Series object the method combine() can be used.

Example:

# Example Python program to append two pandas Series objects

# to get a new Series

import pandas as pds

 

# Create lists of odd numbers

sequence1  = [81, 83, 85, 87, 89];

sequence2  = [91, 93, 95, 97, 99];

 

# Create pandas series instances

series1 = pds.Series(sequence1);

series2 = pds.Series(sequence2);

 

# Append two series to get new series

newSeries = series1.append(series2);

print("New series obtained by appending two series instances:");

print(newSeries);

 

Output:

New series obtained by appending two series instances:

0    81

1    83

2    85

3    87

4    89

0    91

1    93

2    95

3    97

4    99

dtype: int64


Copyright 2024 © pythontic.com