提取句点“.”后的文本 来自 Pandas Dataframes 列中的值

我在数据框中有一列,如下所示:


| Category |

------------

| B5050.88

| 5051.90

| B5050.97Q

| 5051.23B

| 5051.78E

| B5050.11

| 5051.09

| Z5052

我想提取句号后的文本。例如,从B5050.88开始,我只想要“88”;从5051.78E开始,我只想要“78E”;对于 Z50502,这没什么,因为没有句号。


预期输出:


| Category | Digits |

---------------------

| B5050.88 | 88  |

| 5051.90  | 90  |

| B5050.97Q| 97Q |

| 5051.23B | 23B |

| 5051.78E | 78E |

| B5050.11 | 11  |

| 5051.09  | 09  |

| Z5052    | -   |

我尝试使用这个


df['Digits'] = df.Category.str.extract('.(.*)')

但我没有得到正确的答案。使用上述内容,对于 B5050.88,我得到相同的 B5050.88;对于 5051.09,我得到 NaN。如果没有文本,则基本上为 NaN。


紫衣仙女
浏览 108回答 4
4回答

守着一只汪

你可以做splits = [str(p).split(".") for p in df["Category"]]df["Digits"] = [p[1] if len(p)>1 else "-" for p in splits]IEdf = pd.DataFrame({"Category":["5050.88","5051.90","B5050.97","5051.23B","5051.78E","B5050.11","5051.09","Z5052"]})#df#   Category# 0 5050.88# 1 5051.90# 2 B5050.97# 3 5051.23B# 4 5051.78E# 5 B5050.11# 6 5051.09# 7 Z5052splits = [str(p).split(".") for p in df["Category"]]splits# [['5050', '88'], # ['5051', '90'], # ['B5050', '97'], # ['5051', '23B'], # ['5051', '78E'], # ['B5050', '11'], # ['5051', '09'], # ['Z5052']]df["Digits"] = [p[1] if len(p)>1 else "-" for p in splits]df# Category  Digits# 0 5050.88     88# 1 5051.90     90# 2 B5050.97    97# 3 5051.23B    23B# 4 5051.78E    78E# 5 B5050.11    11# 6 5051.09     09# 7 Z5052        -不太漂亮,但很有效编辑:添加了“-”而不是 NaN 和代码片段

摇曳的蔷薇

试试下面:df['Category'].apply(lambda x : x.split(".")[-1] if "." in list(x) else "-")检查下面的代码

凤凰求蛊

其他方式df.Category.str.split('[\.]').str[1]0&nbsp; &nbsp; &nbsp;881&nbsp; &nbsp; &nbsp;902&nbsp; &nbsp; 97Q3&nbsp; &nbsp; 23B4&nbsp; &nbsp; 78E5&nbsp; &nbsp; &nbsp;116&nbsp; &nbsp; &nbsp;097&nbsp; &nbsp; NaN或者df.Category.str.extract('((?<=[.])(\w+))')

LEATH

你需要逃避你的第一个.并做fillna:df["Digits"] = df["Category"].astype(str).str.extract("\.(.*)").fillna("-")print(df)输出:&nbsp; &nbsp; Category Digits0&nbsp; &nbsp;B5050.88&nbsp; &nbsp; &nbsp;881&nbsp; &nbsp; 5051.90&nbsp; &nbsp; &nbsp;902&nbsp; B5050.97Q&nbsp; &nbsp; 97Q3&nbsp; &nbsp;5051.23B&nbsp; &nbsp; 23B4&nbsp; &nbsp;5051.78E&nbsp; &nbsp; 78E5&nbsp; &nbsp;B5050.11&nbsp; &nbsp; &nbsp;116&nbsp; &nbsp; 5051.09&nbsp; &nbsp; &nbsp;097&nbsp; &nbsp; &nbsp; Z5052&nbsp; &nbsp; &nbsp; -
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python