如果字符串包含'XX',然后更改颜色... Write_rich_string为Panda Dataframe?

时间:2018-08-30 13:04:23

标签: python python-3.x python-2.7 pandas numpy

我保存了大约7个df,并将它们写入excel工作簿中的各个工作表。他们目前正在使用openpyxl。我发现这段代码几乎可以完全满足我的要求-问题是,如果我将“ sequences = .....”替换为“ sequences = df”(这是我的数据帧的名称),则不会填充数据正确地..

最终我要在这1个工作簿上(在不同的工作表上)放入7个df,其中一些带有这种条件,有些带有其他条件,有些没有条件。是否有人对如何正确执行此操作有任何想法?我还在学习

from xlsxwriter.workbook import Workbook

workbook = Workbook('sequences.xlsx')
worksheet = workbook.add_worksheet()

red = workbook.add_format({'color': 'red'})
green = workbook.add_format({'color': 'green'})

sequences = [
    'ACAAGATG',
    'CCATTGTC',
    'CCCCGGCC',
    'CCTGCTGC',
    'GCTGCTCT',
    'CGGGGCCA',
    'GGCCACCG',
]

worksheet.set_column('A:A', 40)

for row_num, sequence in enumerate(sequences):

    format_pairs = []

    # Get each DNA base character from the sequence.
    for base in sequence.upper():

        # Prefix each base with a format.
        if base == 'A' or base == 'T':
            format_pairs.extend((red, base))

        elif base == 'G' or base == 'C':
            format_pairs.extend((green, base))

        else:
            # Non base characters are unformatted.
            format_pairs.append(base)

    worksheet.write_rich_string(row_num, 0, *format_pairs)

workbook.close()

这是df的输出: enter image description here

这是我在excel文件中得到的: enter image description here

0 个答案:

没有答案