简体   繁体   English

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

[英]Merging Series to Single Output - pandas

I want a single series as the output and not multiple series like below: 我希望将单个序列作为输出,而不要像下面这样的多个序列:

Current Output: 电流输出:

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

Desired Output: 所需输出:

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

However with my current code I can't get the series to consolidate. 但是,用我当前的代码,我无法使该系列合并。 I have attempted to make it into a dataframe with all the information I want appended to it; 我试图将其添加到要附加所有信息的数据框中。 however, when trying to combine all the dataframes in the output I haven't been able to get it to combine either. 但是,当尝试合并输出中的所有数据帧时,我无法使其合并。 I know I am running a loop before the creation of the dataframe for the regex operator I am doing to some text before creating the strings/dataframe which is most likely causing the multiple outputs. 我知道在为正则表达式运算符创建数据框之前,我正在运行一个循环,在创建字符串/数据框之前,我正在处理一些文本,这很可能导致多个输出。 Is there a way I can combine it post loop? 有没有办法可以在循环后将其合并? Code below: 代码如下:

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)

With Edits: 进行编辑:

    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)

New Output: 新输出:

0    6.99% to 24.99%
dtype: object

Store your values and use pd.concat afterwards 存储您的值,然后使用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))

Then just 然后就

>>> pd.concat(vals)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM