Size, shape, uniqueness, and counts of values

The number of items in a Series object can be determined by several techniques. To demonstrate this, we will use the following Series:

In [16]:
   # example series, which also contains a NaN
   s = pd.Series([0, 1, 1, 2, 3, 4, 5, 6, 7, np.nan])
   s

Out[16]:
   0     0
   1     1
   2     1
   3     2
   4     3
   5     4
   6     5
   7     6
   8     7
   9   NaN
   dtype: float64

The length can be determined using the len() function:

In [17]:
   # length of the Series
   len(s)

Out[17]:
   10

Alternately, the length can be determined using the .size property:

In [18]:
   # .size is also the # of items in the Series
   s.size

Out[18]:
   10

The .shape property returns a tuple where the first item is the number of items:

In [19]:
   # .shape is a tuple with one value
   s.shape

Out[19]:
   (10,)

The number of the values that are not part of the NaN can be found by using the .count() method:

In [20]:
   # count() returns the number of non-NaN values
   s.count()

Out[20]:
   9

To determine all of the unique values in a Series, pandas provides the .unique() method:

In [21]:
   # all unique values
   s.unique()

Out[21]:
   array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,  nan])

Also, the count of each of the unique items in a Series can be obtained using .value_counts():

In [22]:
   # count of non-NaN values, returned max to min order
   s.value_counts()

Out[22]:
   1    2
   7    1
   6    1
   5    1
   4    1
   3    1
   2    1
   0    1
   dtype: int64
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.167.183