[英]Strip strings in pandas columns
rstrip
不適用於正則表達式。 根據文檔,
to_strip str 或無,默認無
指定要刪除的字符集。 這組字符的所有組合都將被去除。 如果沒有,則刪除空格。
>>> "1.76 @ 0.88".rstrip("[\ \@ \d.[0-9]+]")
'1.76 @ 0.88'
>>> "1.76 @ 0.88".rstrip("[\ \@ \d.[0-8]+]") # It's not treated as regex, instead All combinations of characters(`[\ \@ \d.[0-8]+]`) stripped
'1.76'
您可以改用replace
方法。
for col in df.columns[1:]:
df[col] = df[col].str.replace(r"\s@\s[\d\.]+$", "", regex=True)
str.split() 怎么樣? https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html#pandas.Series.str.split
function 使用提供的分隔符將系列拆分為 dataframe 列(當 expand=True 時)。
以下示例拆分 serie df_engine[col] 並生成 dataframe。新 dataframe 的第一列包含值中第一個分隔符“@”之前的值
df_engine[col].str.split('@', expand=True)[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.