英文:
pandas - after combining data from two csvs, it only shows one cell per column
问题
请记住我是新手,对Python和Stack Overflow都不太熟悉!
我有一个CSV文件,需要制作以便将一些数据上传到另一个平台,它需要以这种格式,所有数据都在一个名为'results'的数据框中。在根据条件更改一些数据后,'Collection'列中什么都没有,只有handle和titles列中的一个单元格,其中包含很多不同的数据。没有错误,但是它没有提供期望的输出。[shopify_adapter.csv](https://i.stack.imgur.com/pB3xb.png)
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')
<details>
<summary>英文:</summary>
*please remember I am new to python and stack overflow!*
I have a csv I need to make in order to upload some data to another platform, it needs to be in this format and all the data is on a dataframe called 'results.' After changing some data based on conditions being true, I do not get anything in the 'Collection' column and only one cell in the handle and titles columns, where there is lot's of varying data. There are no errors, but, it does not give the desired output.[shopify_adapter.csv](https://i.stack.imgur.com/pB3xb.png)
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:
(with varying names in the title and artist section)
[desired output from shopify_adapter.csv](https://i.stack.imgur.com/CAC0C.png)
I have tried using different forms of if statements and have tried only modifyng the "result" file.
thanks for any help
</details>
# 答案1
**得分**: 0
在 [`loc`][1] 中,第二个参数应该是列的名称。因此,你应该用以下内容替换 `Collection` 部分:
```python
result.loc[result['Medium'] == 'Original Artwork', 'Collection'] = 'Original Artwork'
result.loc[result['Medium'] != 'Original Artwork', 'Collection'] = 'Prints'
在你的情况下,你正在使用 pd.Series
shopify_adapter['Collection']
中的值作为索引。一个更简单的例子,让你更好地可视化是这样的:
df = pd.DataFrame({'Medium': ['a', 'b'], 'Collection': ['', '']})
# 返回所有 Medium 为 'a' 的行
df.loc[df['Medium'].eq('a')]
# 返回所有 Medium 为 'a' 的行的 'Collection' 列
df.loc[df['Medium'].eq('a'), 'Collection']
# 返回所有 Medium 为 'a' 的行的列 df['Collection']
df.loc[df['Medium'].eq('a'), df['Collection']]
英文:
In loc
, the second argument should be the name of the column. Thus, you should replace the Collection
part with the following:
result.loc[result['Medium'] == 'Original Artwork', 'Collection'] = 'Original Artwork'
result.loc[result['Medium'] != 'Original Artwork', 'Collection'] = 'Prints'
In your case you are using the values in the pd.Series
shopify_adapter['Collection']
as an index. A easier example, so you can visualize better is something like this:
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']]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论