How to create an empty array in Python

Published on Aug. 22, 2023, 12:16 p.m.

There are several ways to create an empty array in Python, depending on the specific data structure you want to create:

  1. For a simple one-dimensional array, you can create an empty list using square brackets:
empty_array = []
  1. For a numpy array, you can use the numpy.empty() function to create an array with a specified shape and data type:
import numpy as np
empty_array = np.empty(shape=(3,4), dtype=int)
  1. For a pandas DataFrame or Series, you can use the constructor function without any arguments to create an empty data structure:
import pandas as pd
empty_dataframe = pd.DataFrame()
empty_series = pd.Series()

Note that in all cases, these empty data structures will not have any values or elements, but will still have the appropriate data type and structure.

Tags: