You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas生成Shopify适配CSV时仅显单列单单元格,Collection列无数据求助

问题分析与解决

核心问题

  1. 初始DataFrame行数不匹配:你创建的shopify_adapter初始仅1行,但result是多行数据,直接赋值列会导致数据无法正确映射,最终只输出单行结果。
  2. Collection列赋值逻辑错误:用result.loc修改shopify_adapter的列不符合pandas操作逻辑,无法正确填充对应值。

修正代码

import pandas as pd

# 基于result的行数初始化shopify_adapter,保证行数匹配
shopify_adapter = pd.DataFrame(index=result.index, columns=[
    'Handle', 'Title', 'Collection', 'Body (HTML)', 'Vendor', 
    'Product Category', 'Type', 'Tags', 'Published', 'Option1 Name', 
    'Option1 Value', 'Option2 Name', 'Option2 Value', 'Option3 Name', 
    'Option3 Value', 'Variant SKU', 'Variant Grams', 'Variant Inventory Tracker', 
    'Variant Inventory Qty', 'Variant Inventor Policy', 'Variant Fulfillment Service', 
    'Variant Price', 'Variant Compare At Price', 'Variant Requires Shipping', 
    'Variant Taxable', 'Variant Barcode', 'Image Src', 'Image Position', 
    'Image Alt Text', 'Gift Card', 'SEO Title', 'SEO Description', 
    'Google Shopping / Google Product Category', 'Google Shopping / Gender', 
    'Google Shopping / Age Group', 'Google Shopping / MPN', 
    'Google Shopping / AdWords Grouping', 'Google Shopping / AdWords Labels', 
    'Google Shopping / Condition', 'Google Shopping / Custom Product', 
    'Google Shopping / Custom Label 0', 'Google Shopping / Custom Label 1', 
    'Google Shopping / Custom Label 2', 'Google Shopping / Custom Label 3', 
    'Google Shopping / Custom Label 4', 'Variant Image', 'Variant Weight Unit', 
    'Variant Tax Code', 'Cost per item', 'Price / International', 
    'Compare At Price / International', 'Status'
])

# 填充Handle和Title列
shopify_adapter["Handle"] = result['Artist'].astype(str) + " - " + result["Title"]
shopify_adapter["Title"] = result['Artist'].astype(str) + " - " + result["Title"]

# 正确填充Collection列:根据result的Medium字段匹配值
shopify_adapter.loc[result['Medium'] == 'Original Artwork', 'Collection'] = 'Original Artwork'
shopify_adapter.loc[result['Medium'] != 'Original Artwork', 'Collection'] = 'Prints'

# 导出CSV,避免导出索引列
shopify_adapter.to_csv('shopify_adapter.csv', index=False)

关键修改点

  • 初始化优化:用result.index创建shopify_adapter的索引,确保两者行数完全一致,后续列赋值能精准对应每一行数据。
  • Collection赋值修正:直接在shopify_adapter上使用loc索引,结合result['Medium']的条件赋值,保证每一行的Collection值匹配正确。
  • 导出优化:添加index=False避免导出pandas索引列,符合Shopify的上传格式要求。

内容的提问来源于stack exchange,提问作者J.O.S.H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 14:27:05