Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen sum() and count() | Section
Data Manipulation with pandas
Abschnitt 1. Kapitel 21
single

single

booksum() and count()

Swipe um das Menü anzuzeigen

pandas offers the count() method, which counts all non-null cells (neither None nor NaN) for each column.

df = pd.read_csv(file.csv)
number_of_cells = df.count()

To find the count of non-null values in a specific column, use the following syntax:

df = pd.read_csv(file.csv)
number_of_cells = df['name of the column'].count()

pandas also provides the sum() method. This method calculates the sum of values for each column, but it only works with numeric or boolean columns.

df = pd.read_csv(file.csv)
total = df.sum()

Since the isna() method returns a boolean DataFrame, you can use the following syntax to calculate the number of missing values for each of the columns:

missing_values_count = df.isna().sum()

To find the sum of values in a particular column, use the following syntax:

df = pd.read_csv(file.csv)
total = df['name of the column'].sum()
Aufgabe

Swipe to start coding

You are given a DataFrame named audi_cars.

  • Get the count of non-null cells in each column and store the result in the number_of_cells variable.
  • Compute the total price (using the 'price' column) for all cars in the DataFrame and store the result in the total_price variable.
  • Identify the number of missing values in each column and store the result in the null_count variable.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 21
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt