Pandas Series API Reference
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 an index.
The following is a common API reference for Pandas Series:
Series constructor
| Method | Description |
|---|---|
pd.Series(data, index, dtype, name, copy) |
Create a Series object, supporting custom data, index, data type, and name. |
Series attributes
| Attribute | Description |
|---|---|
Series.values |
Return the data part of the Series (numpy array). |
Series.index |
Return the index of the Series. |
Series.dtype |
Return the data type 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 occupied 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 (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 the statistical summary of the Series (such as 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. |
Handling unique values
| 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 values. |
Series.sort_index(ascending=True) |
Sort by index. |
Index operations
| Method | Description |
|---|---|
Series.reset_index(drop=False) |
Reset the index. |
Series.drop(labels) |
Delete the elements at the specified index. |
Series.get(key, default=None) |
Get the value at the specified index, and return a default value if it does not exist. |
Series.set_axis(labels) |
Set a new index. |
Data conversion
| Method | Description |
|---|---|
Series.map(arg) |
Map values in the Series according to a passed-in function or dictionary. |
Series.apply(func) |
Apply a function to each element in the Series. |
Series.astype(dtype) |
Convert the Series to a 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 the values of another Series. |
Series.clip(lower, upper) |
Limit the values in the Series to a specified range. |
Series.isin(values) |
Check whether the values in the Series are in a specified list. |
Series.between(left, right) |
Check whether the values in the Series are within a specified range. |
Statistical calculations
| Method | Description |
|---|---|
Series.sum() |
Return the sum of all values in the Series. |
Series.mean() |
Return the average 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 in the Series. |
Series.quantile(q) |
Return the value at the specified quantile in the Series. |
Time series operations
| Method | Description |
|---|---|
Series.dt |
Access datetime attributes (only applicable to Series of datetime type). |
Series.dt.year |
Return the year. |
Series.dt.month |
Return the month. |
Series.dt.day |
Return the date. |
String operations
| Method | Description |
|---|---|
Series.str |
Access string methods (only applicable to Series of string type). |
Series.str.lower() |
Convert the string to lowercase. |
Series.str.upper() |
Convert the string to uppercase. |
Series.str.contains(pattern) |
Check whether the string contains a specified pattern. |
Examples
Examples
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 the first 2 rows
# Handle missing values
s_with_nan = pd.Series([10, None, 30])
print(s_with_nan.fillna(0)) # Fill missing values with 0
# Handle unique values
print(s.nunique()) # Output the number of unique values
# Sort
print(s.sort_values(ascending=False)) # Sort by values in descending order
# Statistical calculations
print(s.mean()) # Output the average
# Create 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
# Handle missing values
s_with_nan = pd.Series([10, None, 30])
print(s_with_nan.fillna(0)) # Fill missing values with 0
# Handle unique values
print(s.nunique()) # Output the number of unique values
# Sort
print(s.sort_values(ascending=False)) # Sort by values in descending order
# Statistical calculations
print(s.mean()) # Output the average
If you need more detailed information, you can refer toPandas official documentation。
Other extensions