简体   繁体   中英

How to convert a series object in to data frame using string cleaning

I have a series object of strings where there is a specific characters i can go along with. For instance, the one with the end character of [] will be corresponded to those with end character of ()

s = pd.Series(['September[jk]', 'firember hfh(start)','secmber(end)','Last day(hjh)',
              'October[jk]','firober fhfh (start)','thber(marg)','lasber(sth)',
              'December[jk]','anober(start)','secber(start)','Another(hkjl)'])

I can simply clean the data but these characters at the end should help me build the resulting data frame like this

0   September   firember hfh
1   September   secmber
2   September  Last day
3    October   firober fhfh
4    October     thber
5    October    lasber
6   December    anober
7   December    secber
8   December   Another

I don't think there's any magic here, so I recommend parsing the list yourself before creating the dataframe:

import re
import pandas as pd

l = ['September[jk]', 'firember hfh(start)','secmber(end)','Last day(hjh)',
              'October[jk]','firober fhfh (start)','thber(marg)','lasber(sth)',
              'December[jk]','anober(start)','secber(start)','Another(hkjl)']

month = None
mylist = []
for i, el in enumerate(l):
    m = re.match('(.*?)\[.*?\]', el)
    if m:
        month = m.groups()[0]
    else:
        m = re.match('(.*?)\(.*?\)', el)
        if m:
            mylist.append({'Month':month, 'Value':m.groups()[0]})
        else:
            print("Cannot find a match for {}".format(el))

df = pd.DataFrame(mylist)
print(df)

Out:

       Month          Value
0  September   firember hfh
1  September        secmber
2  September       Last day
3    October  firober fhfh 
4    October          thber
5    October         lasber
6   December         anober
7   December         secber
8   December        Another

Side note: I used the re library for regex because it could be adapted to many more complex situations, but in your case you could just use the built-in functions, with in and split :

for i, el in enumerate(l):
    if '[' in el:
        month = el.split('[')[0]
    else:
        if '(' in el:
            mylist.append({'Month':month, 'Value':el.split('(')[0]})
        else:
            print("Cannot find a match for {}".format(el))

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