我想从我的pandas数据框架的列中的任意字符串中提取数值。
在列"watt"上循环的两个正则表达式使用str.extract。
str.extract函数应应用于所有NaN值。
在下一次迭代时,非NaN值(=matches)将从str.extract操作中排除,以便保留以前的结果而不覆盖。
我必须完全误解的东西在这里,因为我的实现并不奏效。
虽然我使用.isnan()来过滤掉以前的匹配,但它会覆盖以前的匹配。
import pandas as pd
df = pd.DataFrame([{'title':'This bulb operates at 222 watts and is fabulous.'},
{'title':'This bulb operates at 999 w and is fantastic.'}])
regexes = ['([0-9.,]{1,})[s-]{0,1}watt[s]{0,1} ', '([0-9.,]{1,})[s-]{0,1}w ']
for regex in regexes:
#create column with nan values on first iteration
if 'watt' not in df.columns:
df['watt'] = np.nan
#select only rows from "watt" column with nan values -> run str.extract() -> update df "watt" column, repeat...
print(df[df['watt'].isnull()]['title'].str.extract(regex)) #debug
df['watt'] = df[df['watt'].isnull()]['title'].str.extract(regex)
print(df)
我认为在你的框架中有2个选项:在这两种情况下,你都应该屏蔽NaN
s,在你正在搜索的列以及你正在写的列。
由于.str.extract()
返回expand=False
(默认)的序列,因此写入需要进行一些调优(使用.values
):
regexes = [r'([0-9.,]+)[s-]?watt[s]? ', r'([0-9.,]+)[s-]?w ']
df['watt'] = np.nan
for regex in regexes:
mask = df['watt'].isna()
df.loc[mask, 'watt'] = df.loc[mask, 'title'].str.extract(regex).values
或者您可以在正则表达式中使用命名组,使组名与您要写入的列的标签匹配:
regexes = [r'(?P<watt>[0-9.,]+)[s-]?watt[s]? ', r'(?P<watt>[0-9.,]+)[s-]?w ']
df['watt'] = np.nan
for regex in regexes:
mask = df['watt'].isna()
df.loc[mask, 'watt'] = df.loc[mask, 'title'].str.extract(regex)
都产生以下结果:
title watt
0 This bulb operates at 222 watts and is fabulous. 222
1 This bulb operates at 999 w and is fantastic. 999