Skip to main content

Merge two datasets

You can use the merge function to combine two datasets that share a common column name. By default, all columns in common are used as the merge key, while uncommon column names will be ignored. Also, if you want to use only a subset of the columns in common, rename the other columns so the columns are unique in the merged result.

note

In order for a merge to work in multinode clusters, one of the datasets must be small enough to exist in every node.

import h2o
h2o.init()
import numpy as np

# Create a dataset by inputting raw data.
df1 = h2o.H2OFrame.from_python({'A':['Hello', 'World', 'Welcome', 'To', 'H2O', 'World'],
'n': [0,1,2,3,4,5]})
df1.describe
A n
------- ---
Hello 0
World 1
Welcome 2
To 3
H2O 4
World 5

[6 rows x 2 columns]

# Generate a random dataset from python.
df2 = h2o.H2OFrame.from_python([[x] for x in np.random.randint(0, 10, size=20).tolist()], column_names=['n'])
df2.describe
n
---
nan
0
8
6
1
7
8
5
1
3

[21 rows x 1 column]

# Merge the first dataset into the second dataset. Note that only columns
# in common are merged (i.e, values in df2 greater than 5 will not be merged).
df3 = df2.merge(df1)
df3.describe
n A
--- -------
nan Hello
3 To
3 To
0 Hello
5 World
3 To
0 Hello
5 World
1 World
2 Welcome

[14 rows x 2 columns]

# Merge all of df2 into df1. Note that this will result in missing values for
# column A, which does not include values greater than 5.
df4 = df2.merge(df1, all_x=True)
df4.describe
n A
--- -----
nan Hello
0 Hello
8
6
1 World
7
8
5 World
1 World
3 To

[21 rows x 2 columns]

Feedback