Skip to content
Home » pandas Part 24 – Dataset Concatenation

pandas Part 24 – Dataset Concatenation

Spread the love

DATASET CONCATENATION

We can concatenate Series and DataFrame objects using the pd.concat method. Here’s a simple example of concatenation. Let’s start with two Series objects:

In [3]:
import numpy as np
import pandas as pd

s1 = pd.Series(['A', 'B', 'C'])
s2 = pd.Series(['D', 'E', 'F'])

pd.concat([s1, s2])
Out[3]:
0    A
1    B
2    C
0    D
1    E
2    F
dtype: object

As you can see, the indices overlap. You may want to specify them explicitly to avoid this:

In [4]:
s1 = pd.Series(['A', 'B', 'C'], index=[1, 2, 3])
s2 = pd.Series(['D', 'E', 'F'], index=[4, 5, 6])

pd.concat([s1, s2])
Out[4]:
1    A
2    B
3    C
4    D
5    E
6    F
dtype: object

And now an example with two dataframes. First let’s create a function to quickly create DataFrames. Let’s use a comprehension for that:

In [13]:
def create_df(cols, index):
    x = {c: [str(c) + str(i) for i in index] for c in cols}
    return pd.DataFrame(x, index)
In [14]:
# Let's create the first DataFrame using this function.
df1 = create_df('ABC', [1, 2, 3])
df1
Out[14]:
A B C
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3
In [15]:
# Let's create the second DataFrame.
df2 = create_df('ABC', [4, 5, 6])
df2
Out[15]:
A B C
4 A4 B4 C4
5 A5 B5 C5
6 A6 B6 C6
In [16]:
# And now let's concatenate the two.
pd.concat([df1, df2])
Out[16]:
A B C
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3
4 A4 B4 C4
5 A5 B5 C5
6 A6 B6 C6

As you can see, by default DataFrames are concatenated row-wise. If you want to change this behavior, you can specify the axis for the concatenation to take place along:

In [18]:
# Let's concatenate column-wise.
df1 = create_df('ABC', [1, 2, 3])
df2 = create_df('DEF', [1, 2, 3])

pd.concat([df1, df2], axis=1)
Out[18]:
A B C D E F
1 A1 B1 C1 D1 E1 F1
2 A2 B2 C2 D2 E2 F2
3 A3 B3 C3 D3 E3 F3

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply