Pandas Series Api Reference
Series is a one-dimensional array capable of storing any data type (integers, strings, floats, Python objects, etc.), where each element has a label called an index.
Below is a reference manual for commonly used Pandas Series APIs:
### **Series Constructor**
| **Method** | **Description** |
| --- | --- |
| `pd.Series(data, index, dtype, name, copy)` | Creates a Series object, supporting custom data, index, data type, and name. |
* * *
### **Series Attributes**
| **Attribute** | **Description** |
| --- | --- |
| `Series.values` | Returns the data portion of the Series (numpy array). |
| `Series.index` | Returns the index of the Series. |
| `Series.dtype` | Returns the data type of the Series. |
| `Series.shape` | Returns the shape of the Series (in tuple form). |
| `Series.size` | Returns the number of elements in the Series. |
| `Series.name` | Returns or sets the name of the Series. |
| `Series.empty` | Checks whether the Series is empty. |
| `Series.nbytes` | Returns the number of bytes occupied by the Series. |
| `Series.ndim` | Returns the number of dimensions of the Series (always 1). |
| `Series.hasnans` | Checks whether the Series contains missing values (NaN). |
| `Series.array` | Returns the underlying data of the Series (Pandas array). |
* * *
### **Series Methods**
#### **Data Inspection**
| **Method** | **Description** |
| --- | --- |
| `Series.head(n=5)` | Returns the first n rows of data. |
| `Series.tail(n=5)` | Returns the last n rows of data. |
| `Series.describe()` | Returns statistical summary of the Series (such as count, mean, standard deviation, etc.). |
#### **Missing Value Handling**
| **Method** | **Description** |
| --- | --- |
| `Series.isnull()` | Checks whether each element is a missing value (NaN). |
| `Series.notnull()` | Checks whether each element is not a missing value. |
| `Series.dropna()` | Deletes all missing values. |
| `Series.fillna(value)` | Fills missing values with a specified value. |
#### **Unique Value Handling**
| **Method** | **Description** |
| --- | --- |
| `Series.unique()` | Returns unique values in the Series. |
| `Series.nunique()` | Returns the number of unique values in the Series. |
| `Series.value_counts()` | Returns the frequency of each value in the Series. |
#### **Sorting**
| **Method** | **Description** |
| --- | --- |
| `Series.sort_values(ascending=True)` | Sorts by values. |
| `Series.sort_index(ascending=True)` | Sorts by index. |
#### **Index Operations**
| **Method** | **Description** |
| --- | --- |
| `Series.reset_index(drop=False)` | Resets the index. |
| `Series.drop(labels)` | Deletes elements at specified indices. |
| `Series.get(key, default=None)` | Gets the value at specified index, returns default if not exists. |
| `Series.set_axis(labels)` | Sets a new index. |
#### **Data Conversion**
| **Method** | **Description** |
| --- | --- |
| `Series.map(arg)` | Maps values in the Series based on passed function or dictionary. |
| `Series.apply(func)` | Applies function to each element in the Series. |
| `Series.astype(dtype)` | Converts Series to specified data type. |
| `Series.to_dict()` | Converts Series to dictionary. |
| `Series.to_frame()` | Converts Series to DataFrame. |
| `Series.to_numpy()` | Converts Series to numpy array. |
#### **Data Manipulation**
| **Method** | **Description** |
| --- | --- |
| `Series.copy()` | Copies the Series. |
| `Series.append(to_append, ignore_index)` | Appends another Series. |
| `Series.replace(to_replace, value)` | Replaces values in the Series. |
| `Series.update(other)` | Updates current Series with values from another Series. |
| `Series.clip(lower, upper)` | Limits values in the Series to specified range. |
| `Series.isin(values)` | Checks whether values in the Series are in specified list. |
| `Series.between(left, right)` | Checks whether values in the Series are within specified range. |
#### **Statistical Calculations**
| **Method** | **Description** |
| --- | --- |
| `Series.sum()` | Returns the sum of all values in the Series. |
| `Series.mean()` | Returns the mean of all values in the Series. |
| `Series.median()` | Returns the median of all values in the Series. |
| `Series.min()` | Returns the minimum value in the Series. |
| `Series.max()` | Returns the maximum value in the Series. |
| `Series.std()` | Returns the standard deviation of all values in the Series. |
| `Series.var()` | Returns the variance of all values in the Series. |
| `Series.count()` | Returns the number of non-missing values in the Series. |
| `Series.mode()` | Returns the mode of the Series. |
| `Series.quantile(q)` | Returns the value at specified quantile of the Series. |
#### **Time Series Operations**
| **Method** | **Description** |
| --- | --- |
| `Series.dt` | Accesses datetime attributes (only for Series of datetime type). |
| `Series.dt.year` | Returns the year. |
| `Series.dt.month` | Returns the month. |
| `Series.dt.day` | Returns the day. |
#### **String Operations**
| **Method** | **Description** |
| --- | --- |
| `Series.str` | Accesses string methods (only for Series of string type). |
| `Series.str.lower()` | Converts strings to lowercase. |
| `Series.str.upper()` | Converts strings to uppercase. |
| `Series.str.contains(pattern)` | Checks whether string contains specified pattern. |
* * *
### **Example**
## Example
import pandas as pd
# Create Series
s = pd.Series([10,20,30,40], index=['a','b','c','d'], name='MySeries')
# View data
print(s.head(2))# Output first 2 rows
# Missing value handling
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())# Output number of unique values
# Sorting
print(s.sort_values(ascending=False))# Sort by value in descending order
# Statistical calculations
print(s.mean())# Output mean
* * *
For more detailed information, please refer to the (https://pandas.pydata.org/docs/reference/series.html#).
YouTip