w3resource

Pandas DataFrame: squeeze() function

DataFrame - squeeze() function

The squeeze() function is used to 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.

Syntax:

DataFrame.squeeze(self, axis=None)

Parameters:

Name Description Type/Default Value Required / Optional
axis         A specific axis to squeeze. By default, all length-1 axes are squeezed. {0 or ‘index’, 1 or ‘columns’, None}
Default Value: None
Required

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.

Returns: DataFrame, Series, or scalar
The projection after squeezing axis or all the axes.

Example:


Download the Pandas DataFrame Notebooks from here.

Previous: DataFrame - explode() function
Next: DataFrame - T() function



Share this Tutorial / Exercise on : Facebook and Twitter