简体   繁体   English

Slugify column and parse csv in Python Pandas to a new csv output

[英]Slugify column and parse csv in Python Pandas to a new csv output

Hi I am a newbie to Python and Pandas.. Not sure what I am doing wrong in my code but I am simply trying to convert product name values given in a csv columm to a new output csv, as slug values of the corresponding product names. Hi I am a newbie to Python and Pandas.. Not sure what I am doing wrong in my code but I am simply trying to convert product name values given in a csv columm to a new output csv, as slug values of the corresponding product names . Input is: product-feed.csv输入为:product-feed.csv

product_name产品名称
V-Neck T-Shirt V领T恤
Hoodie with Logo徽标连帽衫
Long Sleeve T-Shirt长袖 T 恤
Hoodie with Pocket带口袋的连帽衫
Hoodie with Zipper拉链连帽衫
Long Sleeve Tee长袖 T 恤
Polo Neck Tee Polo 领 T 恤
V-Neck T-Shirt - Red V 领 T 恤 - 红色
V-Neck T-Shirt - Green V 领 T 恤 - 绿色
V-Neck T-Shirt - Blue V 领 T 恤 - 蓝色

expected output (slugged-output.csv) should be like this when I run the py file in VS Code terminal:当我在 VS Code 终端中运行 py 文件时,预期的 output(slugged-output.csv)应该是这样的:

product_name产品名称
v-neck-t-shirt V领T恤
hoodie-with-logo logo连帽衫
long-sleeve-t-shirt长袖T恤
hoodie-with-pocket带口袋的连帽衫
hoodie-with-zipper拉链连帽衫
long-sleeve-tee长袖三通
polo-neck-tee马球领 T 恤
v-neck-t-shirt-red V领T恤红
v-neck-t-shirt-green V领T恤-绿色
v-neck-t-shirt-blue V领T恤-蓝色

parse_code.py is like this: Note: I am using https://pypi.org/project/python-slugify/ module to pass this to convert the slugs in the code: 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")

The Problem: My output is like this: slugged-output.csv (so as the print in the console)问题:我的 output 是这样的: 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"

Any ideas what I am missing in the code please..thank you:)请给我在代码中遗漏的任何想法..谢谢:)

Eventually after a bit of searching on internet I came across this page and this one line of code resolved everything!!最终在互联网上进行了一些搜索后,我遇到了这个页面,这一行代码解决了一切!

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

DELETED this from my original code and replaced as the line above!从我的原始代码中删除它并替换为上面的行!

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

解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM