pandas 将数据从一个文件复制到模板文件,而不是复制到正确的列

m1m5dgzv  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在写一段代码,它从目录文件中选择数据,并将其复制到模板文件中,而不修改它。
目录文件中的一列(“产品类型”)决定了将数据复制到模板文件中的工作表。
模板文件列标题(我Map到匹配目录文件)在第三行。模板文件中的某些列在目录中没有列,因此这些列应该为空值
虽然Map名称是正确的,但数据没有复制到右列。

import pandas as pd
import openpyxl

# Load the "catalogue" and "template" Excel files
catalogue_file = r"catalogue.xlsx"
catalogue_df = pd.read_excel(catalogue_file , sheet_name="Sheet_all")

template_file = r'template.xlsx'
template_workbook = openpyxl.load_workbook(template_file, data_only=True)

# Create a mapping between column names in "catalogue" and "template"
column_mapping = {
    'PRODUCT ID': 'External ID',
    'model': 'Model Number',
    'Attribute1': 'Characteristic', 
    'Description': 'Model Name',
    'PRODUCT TYPE': 'Product Type', 
}

# Create a mapping between "PRODUCT TYPES" and sheet names in "template"
type_to_sheet_mapping = {
    'PRODUCT_TYPE_1': 'Template-type1',
    'PRODUCT_TYPE_2': 'Template-type2',
    'PRODUCT_TYPE_3': 'Template-type3',
}

for index, row in catalogue_df.iterrows():
    product_type = row['PRODUCT TYPE']  
    data_to_copy = row.rename(column_mapping)

    # Check if the product type has a corresponding sheet name in the mapping
    if product_type in type_to_sheet_mapping:
        sheet_name = type_to_sheet_mapping[product_type]
        try:
        # Select the appropriate sheet in the "template" workbook
            template_sheet = template_workbook[sheet_name]
            # Find the last row and increment by 1
            last_row = template_sheet.max_row
            new_row = [last_row + 1] + data_to_copy.tolist()
            # Add the new data to the sheet
            template_sheet.append(new_row)
        except:
            print(f"{product_type} does not have a template sheet")

template_workbook.save(r"updated_template.xlsx")

字符串

x8diyxa7

x8diyxa71#

我想你可以首先为模板文件中的所有列初始化一个new_row列表,然后遍历数据并将其复制到相应的列。

# ...

for index, row in catalogue_df.iterrows():
    product_type = row['PRODUCT TYPE']  
    data_to_copy = row.rename(column_mapping)

    # Check if the product type has a corresponding sheet name in the mapping
    if product_type in type_to_sheet_mapping:
        sheet_name = type_to_sheet_mapping[product_type]
        try:
            # Select the appropriate sheet in the "template" workbook
            template_sheet = template_workbook[sheet_name]

            # Find the last row and increment by 1
            last_row = template_sheet.max_row
            new_row = [None] * (len(column_mapping) + 1)  # Initialize with None for all columns
            for col_name, value in data_to_copy.items():
                if col_name in column_mapping:
                    col_index = list(column_mapping.values()).index(column_mapping[col_name])
                    new_row[col_index + 1] = value

            # Add the new data to the sheet
            template_sheet.append(new_row)
        except:
            print(f"{product_type} does not have a template sheet")

# ...

字符串

相关问题