pandas – 从两个CSV文件合并数据后,每列只显示一个单元格

huangapple go评论58阅读模式
英文:

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 &#39;results.&#39; After changing some data based on conditions being true, I do not get anything in the &#39;Collection&#39; column and only one cell in the handle and titles columns, where there is lot&#39;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 &quot;result&quot; 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[&#39;Medium&#39;] == &#39;Original Artwork&#39;, &#39;Collection&#39;] = &#39;Original Artwork&#39;
result.loc[result[&#39;Medium&#39;] != &#39;Original Artwork&#39;, &#39;Collection&#39;] = &#39;Prints&#39;

In your case you are using the values in the pd.Series shopify_adapter[&#39;Collection&#39;] as an index. A easier example, so you can visualize better is something like this:

df = pd.DataFrame({&#39;Medium&#39;: [&#39;a&#39;, &#39;b&#39;], &#39;Collection&#39;: [&#39;&#39;, &#39;&#39;]})

# returns all rows with Medium &#39;a&#39;
df.loc[df[&#39;Medium&#39;].eq(&#39;a&#39;)]

# returns only the column &#39;Collection&#39; for all rows with Medium &#39;a&#39;
df.loc[df[&#39;Medium&#39;].eq(&#39;a&#39;), &#39;Collection&#39;]

# returns the columns df[&#39;Collection&#39;] for all rows with Medium &#39;a&#39; 
df.loc[df[&#39;Medium&#39;].eq(&#39;a&#39;), df[&#39;Collection&#39;]]

huangapple
  • 本文由 发表于 2023年4月7日 04:42:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定