Squeeze 1 dimensional axis objects into scalars.
Series or DataFrames with a single element are squeezed to a scalar.
DataFrames with a single column or a single row are squeezed to a
Series. Otherwise the object is unchanged.
This method is most useful when you don’t know if your
object is a Series or DataFrame, but you do know it has just a single
column. In that case you can safely call squeeze to ensure you have a
A specific axis to squeeze. By default, all length-1 axes are
The projection after squeezing axis or all the axes.
Integer-location based indexing for selecting scalars.
Integer-location based indexing for selecting Series.
Inverse of DataFrame.squeeze for a single-column DataFrame.
>>> primes = pd.Series([2, 3, 5, 7])
Slicing might produce a Series with a single value:
>>> even_primes = primes[primes % 2 == 0]
Squeezing objects with more than one value in every axis does nothing:
>>> odd_primes = primes[primes % 2 == 1]
Squeezing is even more effective when used with DataFrames.
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
0 1 2
1 3 4
Slicing a single column will produce a DataFrame with the columns
having only one value:
>>> df_a = df[['a']]
So the columns can be squeezed down, resulting in a Series:
Name: a, dtype: int64
Slicing a single row from a single column will produce a single
>>> df_0a = df.loc[df.index < 1, ['a']]
Squeezing the rows produces a single scalar Series:
Name: 0, dtype: int64
Squeezing all axes will project directly into a scalar: