Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Series API manual

Pandas Series API manual

A Series is a one-dimensional array that can store any data type (integers, strings, floats, Python objects, etc.), and each element has a label called its index.

Below is a manual of commonly used APIs for Pandas Series:

Series constructor

Method Description
pd.Series(data, index, dtype, name, copy) Create a Series object, with support for custom data, index, dtype, and name.

Series attributes

property Description
Series.values Return the data portion of the Series (a numpy array).
Series.index Return the index of the Series.
Series.dtype Return the dtype of the Series.
Series.shape Return the shape of the Series (as a tuple).
Series.size Return the number of elements in the Series.
Series.name Return or set the name of the Series.
Series.empty Check whether the Series is empty.
Series.nbytes Return the number of bytes consumed by the Series.
Series.ndim Return the number of dimensions of the Series (always 1).
Series.hasnans Check whether the Series contains missing values (NaN).
Series.array Return the underlying data of the Series (a Pandas array).

Series methods

Data viewing

Method Description
Series.head(n=5) Return the first n rows of data.
Series.tail(n=5) Return the last n rows of data.
Series.describe() Return a statistical summary of the Series (count, mean, standard deviation, etc.).

Handling Missing Values

Method Description
Series.isnull() Check whether each element is a missing value (NaN).
Series.notnull() Check whether each element is not a missing value.
Series.dropna() Remove all missing values.
Series.fillna(value) Fill missing values with a specified value.

Unique value handling

Method Description
Series.unique() Return the unique values in the Series.
Series.nunique() Return the number of unique values in the Series.
Series.value_counts() Return the frequency of each value in the Series.

Sorting

Method Description
Series.sort_values(ascending=True) Sort by value.
Series.sort_index(ascending=True) Sort by index.

Index operations

Method Description
Series.reset_index(drop=False) Reset the index.
Series.drop(labels) Remove the element at the specified index.
Series.get(key, default=None) Get the value at the specified index, returning a default value if it does not exist.
Series.set_axis(labels) Set a new index.

Data Transformation

Method Description
Series.map(arg) Map the values in the Series according to a supplied function or dictionary.
Series.apply(func) Apply a function to each element of a Series.
Series.astype(dtype) Convert the Series to the specified data type.
Series.to_dict() Convert the Series to a dictionary.
Series.to_frame() Convert the Series to a DataFrame.
Series.to_numpy() Convert the Series to a numpy array.

Data operations

Method Description
Series.copy() Copy the Series.
Series.append(to_append, ignore_index) Append another Series.
Series.replace(to_replace, value) Replace values in the Series.
Series.update(other) Update the current Series with values from another Series.
Series.clip(lower, upper) Clip the values in the Series to a specified range.
Series.isin(values) Check whether values in the Series are in a specified list.
Series.between(left, right) Check whether values in the Series are within a specified range.

Statistical computation

Method Description
Series.sum() Return the sum of all values in the Series.
Series.mean() Return the mean of all values in the Series.
Series.median() Return the median of all values in the Series.
Series.min() Return the minimum value in the Series.
Series.max() Return the maximum value in the Series.
Series.std() Return the standard deviation of all values in the Series.
Series.var() Return the variance of all values in the Series.
Series.count() Return the number of non-missing values in the Series.
Series.mode() Return the mode of the Series.
Series.quantile(q) Return the value at the specified quantile in the Series.

Time series operations

Method Description
Series.dt Access datetime properties (only for Series of datetime type).
Series.dt.year Return the year.
Series.dt.month Return the month.
Series.dt.day Return the day.

String Operations

Method Description
Series.str Access string methods (only for Series of string type).
Series.str.lower() Convert strings to lowercase.
Series.str.upper() Convert strings to uppercase.
Series.str.contains(pattern) Check whether a string contains a specified pattern.

Example

Example

import

pandas

as

pd

Create a Series

s

=

pd.

Series

(

[

10

,

20

,

30

,

40

]

,

index

=

[

'a'

,

'b'

,

'c'

,

'd'

]

,

name

=

'MySeries'

)

View data

print

(

s.

head

(

2

)

)

 

Output the first 2 rows

Handling Missing Values

s_with_nan

=

pd.

Series

(

[

10

,

None

,

30

]

)

print

(

s_with_nan.

fillna

(

0

)

)

 

Fill missing values with 0

Unique value handling

print

(

s.

nunique

(

)

)

 

Print the number of unique values

Sorting

print

(

s.

sort_values

(

ascending

=

False

)

)

 

Sort by value in descending order

Statistical computation

print

(

s.

mean

(

)

)

 

Print the mean


For more detailed information, refer to Pandas official documentation。