Counting Elements in Multidimensional Arrays with Python: Simple Tricks
Learn multiple Python approaches—including NumPy flattening, collections.Counter, and pandas melt/value_counts—to efficiently count the occurrences of each element in both regular and irregular multidimensional arrays, with code examples and a custom flatten function for handling nested structures.
Counting Elements in Multidimensional Arrays
Problem
While counting elements in a one‑dimensional array is straightforward, counting each element in a multidimensional array requires additional techniques.
Thought Process
Common Python libraries do not provide a direct, simple method for multidimensional arrays, so the problem can be solved indirectly by flattening the array first and then counting.
Two‑Dimensional Array
Data preparation using NumPy with a fixed random seed:
import numpy as np
import pandas as pd
np.random.seed(2022) # set random seed
a = np.random.randint(0, 10, (100, 3)) # 100×3 2D arrayCounting elements with collections.Counter after flattening:
from collections import Counter
# Counter cannot directly handle a NumPy array; flatten it first
Counter(a.flatten())
# Or use the flat iterator
Counter(a.flat)Using pandas value_counts after melting the DataFrame:
# Melt the DataFrame to a single column named 'value' and count
pd.DataFrame(a).melt().value_counts('value')Multidimensional Array
Regular (uniform) 3‑D array generation:
np.random.seed(2022)
a = np.random.randint(0, 10, (10, 3, 4)) # 10×3×4 3D arrayFor uniform arrays, .flatten() easily converts them to one dimension, after which counting proceeds as with 2‑D arrays.
Pandas cannot directly convert higher‑dimensional arrays to its DataFrame structure, so the flatten‑then‑count approach remains preferable.
Irregular (jagged) Array
When the array consists of nested lists of varying lengths, numpy.flatten() is insufficient. Example of an irregular nested list:
list_b = [[[1,2,3,4,[1,2,3,4,[1,2,3,4]]],6,7],8,9,[1,2,3,4,[1,2,3,4]],6,7]]
np.array(list_b, dtype='O').flatten()Because the flattened result contains objects, Counter cannot directly count them. A custom recursive flatten function can handle such structures:
def flatten(values):
# Convert to a NumPy flat iterator first
values = np.array(values, dtype='O').flat
for value in values:
if isinstance(value, (list, np.ndarray)):
yield from flatten(value)
else:
yield valueUsing this function, the irregular array can be counted via pandas:
pd.DataFrame(list_b).stack().map(flatten).map(tuple).explode().value_counts()The combination of stack (or melt) and the custom flatten function produces a pandas Series that can be counted with value_counts.
Summary
The article demonstrates several Python‑based methods—NumPy flattening, collections.Counter, and pandas melt/value_counts —to count element frequencies in both regular and irregular multidimensional arrays, and provides a reusable recursive flatten function for complex nested structures.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
