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: Regular Expression 正規表示法 正則表達式 import re ; pattn = “[\d]{4}\/[01][\d]\/[0123][\d] [\d]{6}” ; match = re .search (pattn,text) .group() Python: Regular Expression 正規表示法 正則表達式 import re ; pattn = “[\d]{4}\/[01][\d]\/[0123][\d] [\d]{6}” ; match = re .search (pattn,text) .group()](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220901154435_19.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



![Python: pandas.DataFrame如何移除所有空白列?if df_raw.iloc[r,0] is np.nan: nanLst.append(r) ; df_drop0 = df_raw.drop(nanLst,axis=0) ; pandas.isna() ;df_drop0 = df_raw.drop(nanLst,axis=0).reset_index(drop=True) Python: pandas.DataFrame如何移除所有空白列?if df_raw.iloc[r,0] is np.nan: nanLst.append(r) ; df_drop0 = df_raw.drop(nanLst,axis=0) ; pandas.isna() ;df_drop0 = df_raw.drop(nanLst,axis=0).reset_index(drop=True)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/12/20221206144233_67.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python TQC考題810 最大值與最小值之差,L=[eval(i) for i in s.split()] Python TQC考題810 最大值與最小值之差,L=[eval(i) for i in s.split()]](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/05/20220507094526_36.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言