我想要一个序列作为输出,而不是如下所示的多个序列:
当前输出:
0 5.98% to 35.89%
1 1% to 6%
dtype: object
0 1% to 6%
dtype: object
0 6.99% to 24.99%
1 6.99% to 24.99%
2 6.99% to 24.99%
3 6.99% to 24.99%
dtype: object
0 6.99% to 24.99%
dtype: object
所需的输出:
0 5.98% to 35.89%
1 1% to 6%
0 1% to 6%
0 6.99% to 24.99%
1 6.99% to 24.99%
2 6.99% to 24.99%
3 6.99% to 24.99%
0 6.99% to 24.99%
dtype: object
但是,按照我当前的代码,我无法合并该系列。我试图将其添加到要附加所有信息的数据框中。但是,当尝试合并输出中的所有数据帧时,我无法使其合并。我知道在为正则表达式运算符创建数据框之前,我正在运行一个循环,在创建字符串/数据框之前,我正在处理一些文本,这很可能导致多个输出。 有没有办法在循环后将其组合?下面的代码:
paragraph = soup.find_all(text=re.compile('[0-9]%'))
for n in paragraph:
matches = []
matches.extend(re.findall('(?i)\d+(?:\.\d+)?%\s*(?:to|-)\s*\d+(?:\.\d+)?%', n.string))
sint = pd.Series(matches)
if sint.empty:
continue
print(sint)
进行编辑:
paragraph = soup.find_all(text=re.compile('[0-9]%'))
vals = []
for n in paragraph:
matches = re.findall('(?i)\d+(?:\.\d+)?%\s*(?:to|-)\s*\d+(?:\.\d+)?%', n.string)
vals.append(pd.Series(matches))
sint = pd.concat(vals)
print(sint)
新输出:
0 6.99% to 24.99%
dtype: object
答案 0 :(得分:1)
存储您的值,然后使用pd.concat
paragraph = soup.find_all(text=re.compile('[0-9]%'))
vals = []
for n in paragraph:
matches = re.findall('(?i)\d+(?:\.\d+)?%\s*(?:to|-)\s*\d+(?:\.\d+)?%', n.string)
vals.append(pd.Series(matches))
然后就
>>> pd.concat(vals)