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

5ssjco0h  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(128)
  • 请记住,我是Python和堆栈溢出的新手!*

我有一个csv文件,我需要做,以便上传一些数据到另一个平台,它需要在这种格式和所有的数据是在一个dataframe所谓的“结果”。在改变了一些数据的基础上条件是真的,我没有得到任何东西在“收藏”列,只有一个单元格中的句柄和标题列,那里有很多不同的数据。没有错误,但,它没有给予所需的输出。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')

期望输出:(在标题和艺术家部分有不同的名字)desired output from shopify_adapter.csv
我尝试过使用不同形式的if语句,并且只尝试修改“结果”文件。
谢谢你的帮助

oprakyz7

oprakyz71#

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']]

相关问题