简体   繁体   中英

add a title row on top of each data pairs sets

I have a csv that look like this:

在此处输入图像描述

I use this code:

pairs_ = dict()

with open('text2.txt', 'r') as file:
    for i, j in re.findall(r'(Layer_\d+)\s,\s(\d+\.\d+)ms', file.read()):
        pairs_.setdefault(i, []).extend([i, j])

df = pd.DataFrame(pairs_.values())

to make the pairs aligned in parallel.\

在此处输入图像描述

however I need to add the *** Golden value for Channel[%d], CE[%d] *** on top of every set before the Layer_00

在此处输入图像描述

does anyone know how to achieve it? (the number of sets maybe different, this is just an example of 3 sets)

Just create your headers in a list and pass that to DataFrame, remembering to leave a gap for your intermediate columns. That won't be possible using the mechanism you have now, where you do everything in one re.findall call. You'll have to process line by line so you can extract the headers. This is untested:

pairs_ = dict()
titles = []

with open('text2.txt', 'r') as file:
    for line in file:
        if line.startswith('***'):
            titles.extend( [line.rstrip(),str(len(titles))] )
        else:
            for i, j in re.findall(r'(Layer_\d+)\s,\s(\d+\.\d+)ms', line):
                pairs_.setdefault(i, []).extend([i, j])

df = pd.DataFrame(pairs_.values(), columns=titles )

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