Share on:

Python Pandas For Your Grandpa | Section 2.6 | Series Concatenation
March 18, 2020

Table Of Contents

  1. Introduction
  2. Series
    2.1 Series Creation
    2.2 Series Basic Operations
    2.3 Series Basic Indexing
    2.4 Series Overwriting Data
    2.5 Series Apply
    2.6 Series Concatenation
    2.7 Series Boolean Indexing
    2.8 Series View Vs Copy
    2.9 Series Missing Values
    2.10 Series Challenges

import numpy as np
import pandas as pd

Let’s make some Series.

rg_idx = pd.Series([1, 2, 3, 4])
int_idx = pd.Series([10, 20, 30, 40], index=[0, 1, 2, 3])
float_idx = pd.Series([5, 10, 15, 20], index=[0., 1., 2., 3.])
string_idx = pd.Series([11, 22, 33, 44], index=['0', '1', '2', '3'])

To concatenate two or more Series together, just call pd.concat(), passing in a tuple or list of Series objects.

# Concatenate Series with range index and int index
a = pd.concat(objs=(rg_idx, int_idx))
a
## 0     1
## 1     2
## 2     3
## 3     4
## 0    10
## 1    20
## 2    30
## 3    40
## dtype: int64
a.index
## Int64Index([0, 1, 2, 3, 0, 1, 2, 3], dtype='int64')
# Concatenate Series with range index and float index
b = pd.concat(objs=(rg_idx, int_idx, float_idx))
b
## 0.0     1
## 1.0     2
## 2.0     3
## 3.0     4
## 0.0    10
## 1.0    20
## 2.0    30
## 3.0    40
## 0.0     5
## 1.0    10
## 2.0    15
## 3.0    20
## dtype: int64
b.index
## Float64Index([0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0], dtype='float64')
# Concatenate Series with range index, int index, float index, and string index
c = pd.concat(objs=(rg_idx, int_idx, float_idx, string_idx))
c
## 0       1
## 1       2
## 2       3
## 3       4
## 0      10
## 1      20
## 2      30
## 3      40
## 0.0     5
## 1.0    10
## 2.0    15
## 3.0    20
## 0      11
## 1      22
## 2      33
## 3      44
## dtype: int64
c.index
## Index([0, 1, 2, 3, 0, 1, 2, 3, 0.0, 1.0, 2.0, 3.0, '0', '1', '2', '3'], dtype='object')

When you concatenate Series with different index types, the index gets promoted to the highest level data type in the group.

range < int < float < string/object

The same logic holds true for the Series values, except there’s no range value type like there is with indexes.

int_vals = pd.Series([10, 20, 30, 40])
float_vals = pd.Series([10., 20., 30., 40.])
string_vals = pd.Series(['100', '200', '300', '400'])
x = pd.concat((int_vals, float_vals))
x.dtype
## dtype('float64')
y = pd.concat((int_vals, float_vals, string_vals))
y.dtype
## dtype('O')

Enjoyed this article? Show your support and buy some GormAnalysis merch.
comments powered by Disqus