简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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