Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.squeeze#

DataFrame.squeeze(axis=None)[source]#

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 aSeries. Otherwise the object is unchanged.

This method is most useful when you don’t know if yourobject is a Series or DataFrame, but you do know it has just a singlecolumn. In that case you can safely callsqueeze to ensure you have aSeries.

Parameters:
axis{0 or ‘index’, 1 or ‘columns’, None}, default None

A specific axis to squeeze. By default, all length-1 axes aresqueezed. ForSeries this parameter is unused and defaults toNone.

Returns:
DataFrame, Series, or scalar

The projection after squeezingaxis or all the axes.

See also

Series.iloc

Integer-location based indexing for selecting scalars.

DataFrame.iloc

Integer-location based indexing for selecting Series.

Series.to_frame

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

Examples

>>>primes=pd.Series([2,3,5,7])

Slicing might produce a Series with a single value:

>>>even_primes=primes[primes%2==0]>>>even_primes0    2dtype: int64
>>>even_primes.squeeze()2

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

>>>odd_primes=primes[primes%2==1]>>>odd_primes1    32    53    7dtype: int64
>>>odd_primes.squeeze()1    32    53    7dtype: int64

Squeezing is even more effective when used with DataFrames.

>>>df=pd.DataFrame([[1,2],[3,4]],columns=['a','b'])>>>df   a  b0  1  21  3  4

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

>>>df_a=df[['a']]>>>df_a   a0  11  3

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

>>>df_a.squeeze('columns')0    11    3Name: a, dtype: int64

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

>>>df_0a=df.loc[df.index<1,['a']]>>>df_0a   a0  1

Squeezing the rows produces a single scalar Series:

>>>df_0a.squeeze('rows')a    1Name: 0, dtype: int64

Squeezing all axes will project directly into a scalar:

>>>df_0a.squeeze()1

[8]ページ先頭

©2009-2025 Movatter.jp