pandas 1.4.2

ParametersReturnsBackRef
squeeze(self, axis=None)

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 Series.

Parameters

axis : {0 or 'index', 1 or 'columns', None}, default None

A specific axis to squeeze. By default, all length-1 axes are squeezed.

Returns

DataFrame, Series, or scalar

The projection after squeezing :None:None:`axis` or all the axes.

Squeeze 1 dimensional axis objects into scalars.

See Also

DataFrame.iloc

Integer-location based indexing for selecting Series.

Series.iloc

Integer-location based indexing for selecting scalars.

Series.to_frame

Inverse of DataFrame.squeeze for a single-column DataFrame.

Examples

This example is valid syntax, but we were not able to check execution
>>> primes = pd.Series([2, 3, 5, 7])

Slicing might produce a Series with a single value:

This example is valid syntax, but we were not able to check execution
>>> even_primes = primes[primes % 2 == 0]
... even_primes 0 2 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> even_primes.squeeze()
2

Squeezing objects with more than one value in every axis does nothing:

This example is valid syntax, but we were not able to check execution
>>> odd_primes = primes[primes % 2 == 1]
... odd_primes 1 3 2 5 3 7 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> odd_primes.squeeze()
1    3
2    5
3    7
dtype: int64

Squeezing is even more effective when used with DataFrames.

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
... df a b 0 1 2 1 3 4

Slicing a single column will produce a DataFrame with the columns having only one value:

This example is valid syntax, but we were not able to check execution
>>> df_a = df[['a']]
... df_a a 0 1 1 3

So the columns can be squeezed down, resulting in a Series:

This example is valid syntax, but we were not able to check execution
>>> df_a.squeeze('columns')
0    1
1    3
Name: a, dtype: int64

Slicing a single row from a single column will produce a single scalar DataFrame:

This example is valid syntax, but we were not able to check execution
>>> df_0a = df.loc[df.index < 1, ['a']]
... df_0a a 0 1

Squeezing the rows produces a single scalar Series:

This example is valid syntax, but we were not able to check execution
>>> df_0a.squeeze('rows')
a    1
Name: 0, dtype: int64

Squeezing all axes will project directly into a scalar:

This example is valid syntax, but we were not able to check execution
>>> df_0a.squeeze()
1
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.squeeze

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#870
type: <class 'function'>
Commit: