在组合了来自两个csv的数据后,每列只显示一个单元格



请记住我是python和堆栈溢出的新手!

我有一个csv,我需要使为了上传一些数据到另一个平台,它需要在这种格式,所有的数据是在一个名为"结果"的数据框架。在根据条件为真更改一些数据后,我在"集合"列中没有得到任何东西,并且在句柄和标题列中只有一个单元格,其中有很多不同的数据。没有错误,但是,它没有给出所需的输出。shopify_adapter.csv

shopify_adapter = pd.DataFrame({
'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': ['']
})
shopify_adapter["Handle"] = result['Artist'].astype(str) +" - "+ result["Title"] 
shopify_adapter["Title"] = result['Artist'].astype(str) +"-"+ result["Title"] 
result.loc[result['Medium'] == 'Original Artwork', shopify_adapter['Collection']] = 'Original Artwork'
result.loc[result['Medium'] != 'Original Artwork', shopify_adapter['Collection']] = 'Prints'




shopify_adapter.to_csv('shopify_adapter.csv')

所期望的输出:(在标题和艺术家部分有不同的名字)shopify_adapter.csv的期望输出

我尝试过使用不同形式的if语句,并尝试过只修改"结果"。文件。

谢谢你的帮助

loc中,第二个参数应该是列的名称。因此,您应该将Collection部分替换为以下内容:

result.loc[result['Medium'] == 'Original Artwork', 'Collection'] = 'Original Artwork'
result.loc[result['Medium'] != 'Original Artwork', 'Collection'] = 'Prints'

在您的情况下,您使用pd.Seriesshopify_adapter['Collection']中的值作为索引。一个更容易理解的例子是这样的:

df = pd.DataFrame({'Medium': ['a', 'b'], 'Collection': ['', '']})
# returns all rows with Medium 'a'
df.loc[df['Medium'].eq('a')]
# returns only the column 'Collection' for all rows with Medium 'a'
df.loc[df['Medium'].eq('a'), 'Collection']
# returns the columns df['Collection'] for all rows with Medium 'a' 
df.loc[df['Medium'].eq('a'), df['Collection']]

相关内容