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如何讀寫csv逗點分隔檔(每列內容為新光增有利現金流)?pandas.read_csv(r”路徑\檔名.副檔名”),如何移除list中的nan元素?math.isnan(),如何計算新光增有利IRR?numpy_financial(array) ;輸出csv檔時如何去掉index跟header?如何選擇要寫入的直欄columns? dfFinal.to_csv(fpath, index=False, header=None, columns=[0,1]) Python如何讀寫csv逗點分隔檔(每列內容為新光增有利現金流)?pandas.read_csv(r”路徑\檔名.副檔名”),如何移除list中的nan元素?math.isnan(),如何計算新光增有利IRR?numpy_financial(array) ;輸出csv檔時如何去掉index跟header?如何選擇要寫入的直欄columns? dfFinal.to_csv(fpath, index=False, header=None, columns=[0,1])](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221110122900_3.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: matplotlib繪製出的圖表如何插入背景圖? img = plt.imread(‘background_image.png’) ; ax.imshow(img, extent=[0, 10, -1.2, 1.2], aspect=’auto’, alpha=0.5) Python: matplotlib繪製出的圖表如何插入背景圖? img = plt.imread(‘background_image.png’) ; ax.imshow(img, extent=[0, 10, -1.2, 1.2], aspect=’auto’, alpha=0.5)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230216183536_29.png?quality=90&zoom=2&ssl=1&resize=350%2C233)





近期留言