You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Pandas转换CSV列生成slug格式输出异常问题求助

错误点

str(df["product_name"])是将完整的Pandas Series对象转为字符串,会把整列所有值+格式信息拼接为单个长文本,slugify处理后赋值给整列,自然所有行都是同一个重复值。

修复方案

用Pandas的apply方法将slugify函数逐行应用到每个商品名字符串上即可,修改后的代码如下:

import pandas as pd
from slugify import slugify

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

# 逐行应用slugify,而不是处理整列拼接后的字符串
df["product_name"] = df["product_name"].apply(slugify)

# 调试打印
print(df["product_name"])

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

内容的提问来源于stack exchange,提问作者TheMissingNTLDR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 21:36:05