Differences between revisions 7 and 8
Revision 7 as of 2024-01-15 21:52:28
Size: 3150
Comment: Added methods
Revision 8 as of 2024-01-15 22:30:14
Size: 3706
Comment: Added attrs and methods
Deletions are marked like this. Additions are marked like this.
Line 83: Line 83:
||`index` ||[[Python/Pandas/Types|RangeIndex]] containing the member indices || ||`iloc` ||[[Python/Pandas/Types#A_ILocIndexer|indexable accessor of member values]] ||
||`index` ||[[Python/Pandas/Types|RangeIndex]] containing the member indices      ||
Line 85: Line 86:
||`size` ||[[Python/Builtins/Types#Int|int]] count of member values                                   || ||`loc` ||[[Python/Pandas/Types#A_LocIndexer|indexable accessor of member values]] ||
||`size` ||[[Python/Builtins/Types#Int|int]] count of member values ||
Line 97: Line 99:
||`get` ||return the value from an index or a default ||`s.get("foo", default=None)` ||
Line 100: Line 103:
||`sort_values`||return a sorted `Series` ||`s.sort_values(ascending=False)`|| ||`sort_index` ||return a `Series` sorted by indices ||`s.sort_index(ascending=True)` ||
||`sort_values`||return a `Series` sorted by values ||`s.sort_values(ascending=True)` ||
Line 104: Line 108:

Note that the `get` method can take a list of indices to look up. A new `Series` will be returned if all indices exist, and the singleton default will be returned otherwise.

Python Pandas Series

A Series is an ordered collection of somewhat-uniform data that can be indexed.

The type is fully specified as pandas.core.series.Series.


Example

import pandas as pd

pd.Series(["foo", "bar", "baz"])  # 0   foo
                                  # 1   bar
                                  # 2   baz
                                  # dtype: object


Data Model

A Series can be instantiated with any iterable.

Index

By default, a series is indexed by a sequential integer (beginning at 0).

Certain iterables are interpreted as pairs of indices and values.

d = {"First": "foo", "Second": "bar", "Third": "baz"}
s = pd.Series(d)  # First    foo
                  # Second   bar
                  # Third    baz
                  # dtype: object

A second iterable can be specified as explicit indices.

d = ["foo", "bar", "baz"]
i = ["First", "Second", "Third"]
s = pd.Series(d, i)
s = pd.Series(d, index=i)
s = pd.Series(data=d, index=i)

DType

A series without significant consistency of data types with initialize with a dtype of object. Alternatives include:

  • int64

  • float64

  • datetime64

  • bool

  • category

Dunder Methods

Series objects support all of the dunder methods implied by a sequence, e.g. len() and sorted().


Attributes

Method

Meaning

iloc

indexable accessor of member values

index

RangeIndex containing the member indices

is_unique

bool representing if all member values are unique

loc

indexable accessor of member values

size

int count of member values

values

numpy.ndarray containing the member values


Methods

These methods return numpy.float64 values unless otherwise specified.

Method

Meaning

Example

get

return the value from an index or a default

s.get("foo", default=None)

head

return a Series of the first N member values

s.head(5)

mean

return mean value

product

return product from multiplying all member values

sort_index

return a Series sorted by indices

s.sort_index(ascending=True)

sort_values

return a Series sorted by values

s.sort_values(ascending=True)

std

return standard deviation of values

sum

return sum from adding all member values

tail

return a Series of the last N member values

s.tail(5)

Note that the get method can take a list of indices to look up. A new Series will be returned if all indices exist, and the singleton default will be returned otherwise.


CategoryRicottone