0

我是 Python 和 Pandas 的新手。

不确定我在代码中做错了什么,但我只是想将 csv 列中给出的产品名称值转换为新的输出 csv,作为相应产品名称的 slug 值。

输入是:product-feed.csv

产品名称
V领T恤
徽标连帽衫
长袖 T 恤
带口袋的连帽衫
拉链连帽衫
长袖 T 恤
Polo 领 T 恤
V 领 T 恤 - 红色
V 领 T 恤 - 绿色
V 领 T 恤 - 蓝色

当我在 VS Code 终端中运行 py 文件时,预期的输出(slugged-output.csv)应该是这样的:

产品名称
V领T恤
logo连帽衫
长袖T恤
带口袋的连帽衫
拉链连帽衫
长袖三通
马球领 T 恤
V领T恤红
V领T恤-绿色
V领T恤-蓝色

parse_code.py 是这样的: 注意:我正在使用https://pypi.org/project/python-slugify/模块来传递它来转换代码中的 slug:

import pandas as pd

from slugify import slugify

df = pd.read_csv("product-feed.csv", dtype="str")

df["product_name"] = slugify(str(df["product_name"]))

# VIEW TO DEBUG ONLY
print(df["product_name"])

df["product_name"].to_csv(path_or_buf="slugged-output.csv", index=False, sep=";", quoting=1, encoding="UTF-8")

问题:我的输出是这样的:

slugged-output.csv (如控制台中的打印)

"product_name"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"
"0-v-neck-t-shirt-1-hoodie-with-logo-2-long-sleeve-t-shirt-3-hoodie-with-pocket-4-hoodie-with-zipper-5-long-sleeve-tee-6-polo-neck-tee-7-v-neck-t-shirt-red-8-v-neck-t-shirt-green-9-v-neck-t-shirt-blue-name-product-name-dtype-object"

请给我在代码中遗漏的任何想法..谢谢:)

4

1 回答 1

1

最终在互联网上进行了一些搜索后,我遇到了这个页面,这一行代码解决了一切!

df["product_name"] = df["product_name"].fillna('').apply(lambda x: slugify(x))

从我的原始代码中删除它并替换为上面的行!

df["product_name"] = slugify(str(df["product_name"]))

解决方案

于 2021-11-28T20:56:15.080 回答