w3resource

Pandas Series: squeeze() function

Squeeze 1 dimensional axis objects into scalars

Squeeze 1 dimensional axis Series or DataFrames 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 Series.

Syntax:

Series.squeeze(self, axis=None)
Pandas Series squeeze image
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

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

Example - Slicing might produce a Series with a single value:

Python-Pandas Code:

import numpy as np
import pandas as pd
primes = pd.Series([3, 4, 5, 7])
even_primes = primes[primes % 2 == 0]
even_primes

Output:

1    4
dtype: int64
Pandas Series squeeze image

Python-Pandas Code:

import numpy as np
import pandas as pd
primes = pd.Series([3, 4, 5, 7])
even_primes = primes[primes % 2 == 0]
even_primes.squeeze()

Output:

4

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

Python-Pandas Code:

import numpy as np
import pandas as pd
primes = pd.Series([3, 4, 5, 7])
odd_primes = primes[primes % 2 == 1]
odd_primes

Output:

0    3
2    5
3    7
dtype: int64

Python-Pandas Code:

import numpy as np
import pandas as pd
primes = pd.Series([3, 4, 5, 7])
odd_primes = primes[primes % 2 == 1]
odd_primes.squeeze()

Output:

0    3
2    5
3    7
dtype: int64

Example - Squeezing is even more effective when used with DataFrames:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df

Output:

  x	y
0	2	3
1	4	5

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

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df_a = df[['x']]
df_a

Output:

  x
0	2
1	4

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

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df_a = df[['x']]
df_a.squeeze('columns')

Output:

0    2
1    4
Name: x, dtype: int64

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

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df_0a = df.loc[df.index < 1, ['x']]
df_0a

Output:

  x
0	2

Example - Squeezing the rows produces a single scalar Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df_0a = df.loc[df.index < 1, ['x']]
df_0a.squeeze('rows')

Output:

x    2
Name: 0, dtype: int64

Example - Squeezing all axes will project directly into a scalar:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 3], [4, 5]], columns=['x', 'y'])
df_0a = df.loc[df.index < 1, ['x']]
df_0a.squeeze()

Output:

2

Previous: Repeat elements of a Pandas series
Next: Create a new view of Pandas Series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-squeeze.php