使用 Django Pandas 读取两个 Excel 标头

read two excel headers using django pandas

提问人:Lucas Eduardo 提问时间:11/9/2023 最后编辑:OCaLucas Eduardo 更新时间:11/16/2023 访问量:24

问:

从 excel 表格中,我需要使用 pandas 读取预算。

输入数据
在前两行中,是带有标题和内容的列,这些列将用作要保存在 Sorder 类中的对象。 在第三行中,我有一个产品项的新标题。

EXCEL订单

client  payment_cond    interest_discount_table price_list  sorder_date requested_delivery_date
21101197111186  30/49   MT90    TEC9    01/11/2023  08/11/2023

product gross_price quantity            
LAAAAA1054  2   2           
LAAAAA0962  3   3

问题:
我无法让熊猫识别带有项目的新标题。

if excel_file.name.endswith('.xls') or excel_file.name.endswith('.xlsx'):
    try:
        df = pd.read_excel(excel_file)
        print("first two lines:")
        print(df.iloc[:1])
        print("third line onwards:")
        df = pd.read_excel(excel_file, header=2)
        base_row = df.iloc[2]
        print(df.iloc[2:])
        for index, row in df.iloc[2:].iterrows():
           product = row['product']
           gross_price = row['gross_price']
           qtd = row['qtd']
           product_base = base_row['product']
           gross_price_base = base_row['gross_price']
           qtd_base = base_row['qtd']
           print(f"product: {product}, gross_price: {gross_price}, qtd: {qtd}")
Django Pandas Excel 数据帧 Django-视图

评论


答:

0赞 jehad Rehili 11/9/2023 #1

我认为您在使用标头并使用 iloc 方法时正在复制自己。它将从数据帧中删除行。

if excel_file.endswith('.xls') or excel_file.endswith('.xlsx'): 
    try: 
        df = pd.read_excel(excel_file)
    except:
        return 

    print("first two lines:")
    print(df.iloc[:1])

    print("third line onwards:")

    df = pd.read_excel(excel_file, header=2)

    base_row = df.iloc[1]

    for index, row in df.iterrows():
        product = row['product']
        gross_price = row['gross_price']
        qtd = row['quantity']
            
        product_base = base_row['product']
        gross_price_base = base_row['gross_price']
        qtd_base = base_row['quantity']

        print(
            f"product: {product}, gross_price: {gross_price}, qtd: {qtd}")

评论

0赞 OCa 11/16/2023
你好。您是否能够使用提供的输入数据验证结果?