用Python编写Excel表

时间:2018-01-04 08:48:55

标签: python excel xlsxwriter

首先,我试图从一个文件夹中读出我的数据(它有效)。

接下来,我希望将表格左侧的动物名称和所有者的名称作为标题。

在它们之间的差距应该是每个人拥有多少这些动物的数量。

import matplotlib as mpl
import pandas as pd
import glob
import xlsxwriter
import xlwt

tab_data_folder = "C:\\Users\\Hanna\\Document\\Private\\Animals"
tab_files = glob.glob('%s/Names_*.tab'%(tab_data_folder))

workbook = xlsxwriter.Workbook('table.xlsx')
worksheet = workbook.add_worksheet()

for i, tab_file in enumerate(tab_files):
    df=pd.DataFrame.from_csv(tab_file, sep='\t')
    s_id = file_name[:-4].replace('Name_','')
    df = pd.DataFrame.from_csv(tab_file, sep='\t')

    Animal_names = ['dog', 'cat', 'bird', 'fish', 'pig', 
                    'snake', 'frog', 'monkey', 'zebra', 'sheep']

# Owner_names = tab_file

# calculates the amount of each animal 
    x = []
    number_per_animal = {}
    for b in Animal_names:    
        counter = 0
        number_per_animal[b] = 0
        for c2 in df.animal:
            if c2 == b:
                counter +=1
                number_per_animal[b] += 1
        x.append(counter)
        print ((str(b)+':'+str(x[-1]))) 


    # Add a bold format to use to highlight cells
    bold = workbook.add_format({'bold': True})

    # Write some data headers.
    worksheet.write('B1', s_id, bold)

    # Start from the first cell below the headers.
    row = 1
    col = 0    

    # Iterate over the data and write it out row by row.
    for animal, amount in zip(Animal_names,x):
        worksheet.write(row, col,     animal)
        worksheet.write(row, col + 1, amount)
        row += 1

现在我在第一行收到我的animal_names,第二行包含数据名称Amount作为标题,但我有多个项目,所以我想改进这个Excel文件

现在我希望我的程序将所有其他数据打印为下一行中金额的标题。我知道我需要一个循环或类似的东西,但我不知道它应该在哪里,或者它应该如何 有人有想法吗?

0 个答案:

没有答案