python
numpy
pandas
dataframe
append

python - how to append numpy array to a pandas dataframe

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Appending a NumPy array to a pandas DataFrame usually means converting the array into a DataFrame first and then combining the two objects. In modern pandas, the old DataFrame.append() method is no longer the recommended answer, so the normal tool is pd.concat().

Row Append Versus Column Append

Before writing code, decide what "append" means:

  • add new rows to the bottom of the DataFrame
  • add new columns to the right side of the DataFrame

The shape requirements are different for each case.

Appending Rows

If the NumPy array represents extra rows, it must have the same number of columns as the target DataFrame. Convert the array into a DataFrame with matching column names, then concatenate along axis 0.

python
1import numpy as np
2import pandas as pd
3
4df = pd.DataFrame({
5    "name": ["Ada", "Linus"],
6    "score": [95, 88],
7})
8
9arr = np.array([
10    ["Grace", 91],
11    ["Ken", 84],
12], dtype=object)
13
14arr_df = pd.DataFrame(arr, columns=df.columns)
15result = pd.concat([df, arr_df], ignore_index=True)
16
17print(result)

Using ignore_index=True gives the combined DataFrame a clean sequential index.

Appending Columns

If the array contains new columns, the number of rows must match the existing DataFrame length. In that case, concatenate along axis 1.

python
1import numpy as np
2import pandas as pd
3
4df = pd.DataFrame({
5    "name": ["Ada", "Linus", "Grace"],
6})
7
8arr = np.array([
9    [95, "pass"],
10    [88, "pass"],
11    [72, "pass"],
12], dtype=object)
13
14arr_df = pd.DataFrame(arr, columns=["score", "status"])
15result = pd.concat([df, arr_df], axis=1)
16
17print(result)

Now the array becomes extra columns instead of extra rows.

Why append() Is Not the Modern Answer

Older code often uses:

python
df = df.append(other_df)

That style is outdated. pd.concat() is the modern, explicit, and supported way to combine DataFrames in current pandas versions.

It also scales better when you combine many pieces at once.

Build the Intermediate DataFrame Carefully

The conversion step matters because a NumPy array does not carry pandas column labels. If you skip the column names, pandas may create default numeric labels such as 0, 1, and 2, which often do not match the target DataFrame.

A good pattern is:

  1. inspect the shape of the array
  2. decide whether it represents rows or columns
  3. build a DataFrame with explicit labels
  4. concatenate with the correct axis

That prevents most alignment bugs.

Dtype Considerations

A NumPy array has one dtype for the whole array, while a pandas DataFrame can have different dtypes per column. Mixed data often becomes dtype=object in NumPy.

For example:

python
1arr = np.array([
2    ["Grace", 91],
3    ["Ken", 84],
4], dtype=object)

That is fine, but you may want to cast numeric columns afterward:

python
result["score"] = result["score"].astype(int)

Being explicit keeps downstream code predictable.

Performance Tip for Loops

If you are repeatedly appending arrays in a loop, do not concatenate on every iteration. That causes repeated DataFrame allocations.

A better approach is:

  • collect temporary DataFrames in a list
  • call pd.concat() once at the end

This pattern is much more efficient for larger workloads.

Common Pitfalls

The biggest mistake is using the removed DataFrame.append() pattern in modern pandas code. Prefer pd.concat().

Another mistake is mismatching shapes. Row appends require matching column counts, while column appends require matching row counts.

People also forget to assign column names when converting the NumPy array to a DataFrame, which leads to surprising alignment results.

Finally, mixed-type arrays often become object dtype. If a column should be numeric, cast it explicitly after concatenation.

Summary

  • Convert the NumPy array into a DataFrame before combining it with pandas data.
  • Use pd.concat() instead of the old append() method.
  • Concatenate with axis=0 for rows and axis=1 for columns.
  • Match column labels and shapes carefully.
  • If you combine many pieces, collect them first and concatenate once for better performance.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.