【发布时间】:2020-08-09 06:59:17
【问题描述】:
所以,我正在尝试使用正则表达式从我的 pandas 数据框的一列中提取权重值...由于某种原因,它的提取不正确。
all_data["name"].iloc[0] = "220 grams" # this is purely to show my issue
pattern = "[0-9]+ ?(gram|mg|Gram|GRAM)"
gram_values = all_data["name"].str.contains(pattern)
re.search(pattern, all_data["name"].iloc[0])
输出是
<re.Match object; span=(0, 8), match='220 gram'>
正如预测的那样,它正在出口 220 克。万岁。
现在,如果我使用 pandas.str.extract 方法...
all_data["name"].str.extract(pattern)
那么输出就是
相同的正则表达式模式,两个不同的输出。那么我到底在做什么错呢?正则表达式字符串如何提取不同的值?
【问题讨论】: