This is a concise and comprehensive cheat sheet that focuses on Series in Pandas. It will help you quickly look up attributes, methods, syntaxes, and popular operations when solving problems related to series. You can bookmark this page for future use.
Creating a Series
import pandas as pd
# Create a Series from a list
my_list = [1, 2, 3]
my_series = pd.Series(my_list)
# Create a Series from a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_series = pd.Series(my_dict)
# Create a Series with custom index
my_list = [1, 2, 3]
my_index = ['a', 'b', 'c']
my_series = pd.Series(my_list, index=my_index)
# Create a Series with a specified data type
my_list = [1, 2, 3]
my_series = pd.Series(my_list, dtype='float64')
# Create a Series with a specified name
my_list = [1, 2, 3]
my_series = pd.Series(my_list, name='my_series')
Accessing elements in a Series
# Access an element by index
my_series['a']
# Access multiple elements by index
my_series[['a', 'b']]
# Access an element by position
my_series.iloc[0]
# Access multiple elements by position
my_series.iloc[[0, 1]]
# Access the first n elements
my_series.head(n=2)
# Access the last n elements
my_series.tail(n=2)
Filtering a Series
# Filter a Series by value
my_series[my_series > 1]
# Filter a Series by index
my_series.loc[['a', 'b']]
# Filter a Series using a boolean mask
my_mask = [True, False, True]
my_series[my_mask]
Applying functions to a Series
# Apply a function to every element of a Series
my_series.apply(lambda x: x ** 2)
# Apply a function to every element of a Series and return a new Series
my_new_series = my_series.transform(lambda x: x ** 2)
# Apply a function to the cumulative elements of a Series
my_series.cumsum()
# Apply a function to the rolling window of a Series
my_series.rolling(window=2).sum()
Combining multiple Series
# Combine two Series with the same index
my_series1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
my_series2 = pd.Series([4, 5, 6], index=['a', 'b', 'c'])
my_new_series = my_series1 + my_series2
# Combine two Series with different indexes
my_series1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
my_series2 = pd.Series([4, 5, 6], index=['b', 'c', 'd'])
my_new_series = my_series1.add(my_series2, fill_value=0)
# Concatenate multiple Series
my_series1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
my_series2 = pd.Series([4, 5, 6], index=['d', 'e', 'f'])
my_series3 = pd.concat([my_series1, my_series2])
Other useful attributes and functions
# Get the number of elements in a Series
len(my_series)
# Get the data type of a Series
my_series.dtype
# Get the index of a Series
my_series.index
# Get the values of a Series
my_series.values
# Get descriptive statistics of a Series
my_series.describe()
# Check if a value is in a Series
'value' in my_series
# Check if any value in a Series is null
my_series.isnull().any()
# Check if any value in a Series is not null
my_series.notnull().any()
# Drop null values from a Series
my_series.dropna()
# Fill null values in a Series with a specified value
my_series.fillna(value=0)
# Sort a Series by value
my_series.sort_values()
# Sort a Series by index
my_series.sort_index()
# Reset the index of a Series
my_series.reset_index()
# Rename the index of a Series
my_series.rename(index={'a': 'A', 'b': 'B', 'c': 'C'})
See also: Pandas DataFrame Cheat Sheet
Afterword
This cheat sheet is a quick reference guide that provides a concise and organized summary of the most important concepts, functions, and features of Series in Pandas. I also use it regularly in my work to save time and increase work efficiency. Hope that it will be useful to you as well. If you have any questions related to Pandas, feel free to leave comments. I’m more than happy to hear from you.