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.
例如: 想要使用Series.str.split()
Series.str.contains()
只有Series才能做
但Series跟單欄的DataFrame
並不好分辨
就可以使用squeeze()
確認資料型態必為Series
(若Series只有一個資料,會壓縮成scalar)
df [6001 rows x 3 columns]:

df[“Freq(Hz)”]
其實已經是Series:

df[“Freq(Hz)”].squeeze()
對Series再壓縮一次
do nothing:

df[[“Freq(Hz)”]]
如果誤用[[ ]] (雙層方括弧)
(內層的[]是list的意思,
外層的[]則是定位的意思)
那會得到單欄的DataFrame:

df[[“Freq(Hz)”]].squeeze()
會將單欄的DataFrame
壓縮成Series:

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.
對於兩欄以上的DataFrame做壓縮
do nothing:

ser[ ser==60000000000 ]
(只有單一資料的Series):

其實看60000000000
左邊還帶有index 0
也知道type是Series
但只有單一資料
ser[ ser==60000000000 ].squeeze()
對只有單一資料的Series做壓縮
得到scalar:

推薦hahow線上學習python: https://igrape.net/30afN
df有兩欄,
第0欄為頻率,第1欄為Gain:

df_set_index = df.set_index(“frequency(MHZ) of WR28”)
df_set_index是單欄的DataFrame還是Series?

df_set_index是只有單欄的DataFrame
若需要其為Series型態
可以使用squeeze()
ser=df_set_index.squeeze()

推薦hahow線上學習python: https://igrape.net/30afN


![Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS] Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS]](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2026/02/20260210083748_0_a7d9bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




![Python 進階實戰:深入 Word 核心,挖出那一坨 BLOB (含自省 Debug 技巧, BLOB= Binary Large Object) ; part = doc.part.rels[rid].target_part ; return part.blob if “ImagePart” in type(part).__name__ else None Python 進階實戰:深入 Word 核心,挖出那一坨 BLOB (含自省 Debug 技巧, BLOB= Binary Large Object) ; part = doc.part.rels[rid].target_part ; return part.blob if “ImagePart” in type(part).__name__ else None](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2026/01/20260126111046_0_cd8751.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言