将系列合并为单输出 - 熊猫



我想要一个系列作为输出,而不是像下面这样的多个系列:

电流输出:

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

存储您的值并在之后使用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)

最新更新