我有一個資料框,其中一行如下所示:
Section Title ...
==========================================
4.1.1 4.1.1 Requirements allocation. ...
4.1.2 4.1.2 Safety. ...
4.1.3 4.1.3 Warnings. ...
我正在嘗試計算 Section 列中的句點 (.) 的數量,所以我寫了這一行:
df['Subsections'] = df.Section.str.count(".")
但是,subsections 列回傳的數字是 5,而不是我期望的第一條記錄的數字,即 2,因為有兩個句點 (.)。我在這里缺少一些細微差別嗎?
uj5u.com熱心網友回復:
通過設計將引數Series.str.count(pat, flags=0)解釋 pat為正則運算式模式(參見源代碼)。所以你需要明確地轉義.字符使用\來逐字匹配.
>>> df.Section.str.count("\.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510285.html
