Examples
import numpy as np
import pandas as pd
primes = pd.Series([3, 5, 7, 9])
Slicing might produce a Series with a single value:
even_primes = primes[primes % 3 == 0]
even_primes
even_primes.squeeze()
Squeezing objects with more than one value in every axis does nothing:
odd_primes = primes[primes % 2 == 1]
odd_primes
odd_primes.squeeze()
Squeezing is even more effective when used with DataFrames:
df = pd.DataFrame([[2, 3], [4, 5]], columns=['p', 'q'])
df
Slicing a single column will produce a DataFrame with the columns
having only one value:
df_p = df[['p']]
df_p
Columns can be squeezed down, resulting in a Series:
df_p.squeeze('columns')
df_0p = df.loc[df.index < 1, ['p']]
df_0p
Squeezing the rows produces a single scalar Series:
df_0p.squeeze('rows')
Squeezing all axes will project directly into a scalar:
df_0p.squeeze()