liveBook · Manning

Size attribute

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

The pandas size attribute reports the total number of values in a Series or DataFrame, including missing values.

The pandas size attribute counts every value in a Series or DataFrame, including NaN values. Unlike count, it does not ignore missing entries and is an attribute rather than a method. section 5.2

Count all values in a cleaning task

After loading a parking-violations DataFrame, use df.size when the question concerns the total number of stored values, including missing ones. For a Series, np.size(s) provides the same kind of total-value count. If the question is only how many rows exist, compare len(df.index), which is preferred here for speed. section 5.2

Keep these counting choices separate:

  • Use size for all values, including NaN, across Series and DataFrame objects. section 5.2
  • Use count when missing values should be excluded, while recognizing its performance drawback on large DataFrames. section 5.2
  • Use len(df.index) when the goal is the total number of DataFrame rows. section 5.2

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Defines size and contrasts it with count, len, and shape[0], but does not develop its implementation internals.
Examples
Uses an NYC parking-violations DataFrame and contrasts total-value counting with row counts during missing-data cleanup.
What it assumes
Assumes familiarity with pandas Series, DataFrame objects, missing values, and basic row-counting operations.
What you can do after
Apply it via pandas to count Series or DataFrame values without excluding missing entries.
How it connects
Connects size with Series, DataFrame, NaN values, count, len, and the shape attribute.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.