我的資料框中有一列包含如下文本:
Sunny, with a high near 82. Light and variable wind becoming northwest 5 to 7 mph in the afternoon.
但有時包含如下文字:
A 50 percent chance of showers. Partly sunny, with a high near 61.
我想對其進行操作,以便在洗掉所有其他資訊的同時保留溫度值(即 82 或 61)。所以它會變成“82”或“61”。我無法在固定索引上執行此操作,因為資料框條目的長度是可變的,數字長度也是可變的,因為它是溫度。
我想使用“高附近”、“低附近”等短語來決議字串以找到溫度值。有沒有一種令人愉快的方式來實作這一點?
uj5u.com熱心網友回復:
嘗試這個:
df['temperature'] = df['text'].str.extract('(?:high|low) near (\d )')[0]
輸出:
>>> df
text temperature
0 Sunny, with a high near 82. Light and variable... 82
1 A 50 percent chance of showers. Partly sunny,... 61
uj5u.com熱心網友回復:
您可以使用帶有像 near (\d ) 這樣的大熊貓的正則運算式,它會在 Near 之后找到數字
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/374758.html
