[英]Pandas - Extract value from column in data frame
我有一個 pandas 數據框,列中有很長的文本。 我想 select 所有包含 ABC 的列。 我能夠使用以下方法做到這一點
df[df['Column'].str.contains('ABC', na=False)]
之后我想做的是從該字段中提取包含前綴和接下來的 5 個字母的所有值。 S.所以在找到一個專欄后,我想得到 ABC1234 或 ABC7899。
我希望這是有道理的。
您可以將str.extract
與一個正則表達式一起使用,該正則表達式表示捕獲它看到帶有 5 個后續數字的 ABC 的任何時間
df = pd.DataFrame({'Column':['ABC12345 is in this column', 'Not in this one CCD11111','Also in this one ABC99882']})
df['capture'] = df.Column.str.extract('(ABC\d{5})')
df.dropna(inplace=True)
print(df)
Output
Column capture
0 ABC12345 is in this column ABC12345
2 Also in this one ABC99882 ABC99882
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.