0Pricing
Pandas & NumPy Academy · Lektion

Aggregationsfunktionen

Berechnen Sie Summe, Mittelwert, Minimum, Maximum und Standardabweichung über ein gesamtes Array oder entlang einer bestimmten Achse.

Aggregationsfunktionen ist eine kostenlose Pandas & NumPy Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Pandas & NumPy Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What Is Aggregation?

Aggregation boils an array down to a few summary values, like a total or an average. NumPy's built-in functions do this in fast C, no loops needed.

import numpy as np

a = np.array([3, 7, 2, 9, 1, 6])
print('sum:', a.sum())     # 28
print('mean:', a.mean())   # 4.666...
print('max:', a.max())     # 9

sum() and cumsum()

Use a.sum() for the total of every element, and np.cumsum for a running total — each spot holds the sum of everything up to it.

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a.sum())       # 15
print(np.cumsum(a))  # [ 1  3  6 10 15]

mean() and std()

a.mean() gives the average and a.std() the standard deviation. By default std divides by N; pass ddof=1 when your data is a sample.

import numpy as np

a = np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])
print('mean:', a.mean())           # 5.0
print('std (pop):', a.std())       # 2.0
print('std (sample):', a.std(ddof=1))  # 2.138...

min() and max() with argmin/argmax

a.min() and a.max() give the smallest and largest values. Want their positions instead? argmin and argmax return the index of each.

import numpy as np

a = np.array([3, 7, 2, 9, 1, 6])
print('min:', a.min(), 'at index', a.argmin())  # 1 at 4
print('max:', a.max(), 'at index', a.argmax())  # 9 at 3

Aggregating Along an Axis

The axis argument picks which dimension to collapse: axis=0 gives one value per column, axis=1 one per row. Leave it off and you reduce the whole array.

import numpy as np

m = np.array([[1, 2, 3],
              [4, 5, 6]])

print(m.sum(axis=0))  # [5 7 9]  -- column sums
print(m.sum(axis=1))  # [ 6 15]  -- row sums
print(m.mean(axis=0)) # [2.5 3.5 4.5]

keepdims=True for Shape Preservation

Aggregating along an axis drops that dimension. Add keepdims=True to keep it as size 1 — essential when you want the result to broadcast back later.

import numpy as np

m = np.array([[1, 2, 3],
              [4, 5, 6]])

row_sums = m.sum(axis=1, keepdims=True)
print(row_sums.shape)  # (2, 1)

normed = m / row_sums
print(normed.round(3))
# [[0.167 0.333 0.5  ]
#  [0.267 0.333 0.4  ]]

NaN-Safe Aggregations

One NaN makes a normal sum or mean return NaN. The NaN-safe versions — np.nansum, np.nanmean, and friends — simply skip the missing values.

import numpy as np

a = np.array([1.0, np.nan, 3.0, np.nan, 5.0])
print(a.sum())          # nan
print(np.nansum(a))     # 9.0
print(np.nanmean(a))    # 3.0
print(np.nanmax(a))     # 5.0

np.median and np.percentile

np.median gives the middle value and shrugs off outliers far better than the mean. np.percentile finds the value below which a given percent of data sits.

import numpy as np

a = np.array([1, 2, 3, 4, 100])  # outlier at 100
print('mean:', a.mean())          # 22.0  -- skewed
print('median:', np.median(a))    # 3.0   -- robust
print(np.percentile(a, [25, 50, 75]))  # [ 2.  3.  4.]

np.any() and np.all()

np.any returns True if at least one element passes a condition; np.all returns True only if every element does. Both can check per row or column.

import numpy as np

a = np.array([1, -2, 3, -4])
print(np.any(a < 0))    # True   (some are negative)
print(np.all(a > 0))    # False  (not all are positive)

# Row-wise check on 2D
m = np.array([[1, 2], [-1, 3]])
print(np.all(m > 0, axis=1))  # [ True False]

np.count_nonzero()

np.count_nonzero counts how many elements aren't zero — or how many are True. Pass it a condition like a > 5 to count matches cleanly.

import numpy as np

a = np.array([3, 0, 7, 0, 2, 0])
print(np.count_nonzero(a))         # 3
print(np.count_nonzero(a > 2))     # 2  (elements 3 and 7)

m = np.array([[0, 1], [1, 1]])
print(np.count_nonzero(m, axis=0)) # [1 2]

np.unique() for Distinct Values

np.unique returns the sorted distinct values. Add return_counts=True and you also get how often each appears — an instant frequency table.

import numpy as np

a = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
uniq, counts = np.unique(a, return_counts=True)
print(uniq)    # [1 2 3 4 5 6 9]
print(counts)  # [2 1 2 1 2 1 1]

Quick Check

Test your understanding of NumPy aggregation functions from this lesson.

Lesson Recap

Great job! NumPy aggregations summarise arrays in fast C, the axis argument picks what to collapse, and NaN-safe versions skip missing values. Next: broadcasting.

Häufig gestellte Fragen

Ist die Lektion „Aggregationsfunktionen“ kostenlos?

Ja — der vollständige Text von „Aggregationsfunktionen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Pandas & NumPy Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Pandas & NumPy Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Aggregationsfunktionen“?

Berechnen Sie Summe, Mittelwert, Minimum, Maximum und Standardabweichung über ein gesamtes Array oder entlang einer bestimmten Achse. Du übst Pandas & NumPy Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Pandas & NumPy Academy zu starten?

Keine Vorkenntnisse erforderlich. Pandas & NumPy Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Aggregationsfunktionen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Pandas & NumPy Academy-Lektion Code schreiben und ausführen?

Ja. Jede Pandas & NumPy Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Universelle Funktionen (ufuncs)
  2. Aggregationsfunktionen
  3. Broadcasting-Regeln
  4. Boolesche Maskierung und Fancy Indexing
← Zurück zu Pandas & NumPy Academy