2019-11-28 | Data Engineering 中文
Pandas 列操作:新增、删除与重命名时保留数据契约
bibi45
列变更看似简单,但会影响下游 schema、类型与空值处理。
This article is currently available in Chinese. The language switch changes the site interface; the original text is shown below.
新增或重命名列后,应检查类型、缺失值和下游依赖,而不是只看 DataFrame 是否能打印。
df = df.rename(columns={"created": "created_at"})
df["source"] = "legacy-import"
df = df.drop(columns=["temporary_note"], errors="ignore")
优先返回新 DataFrame 或明确赋值,避免难以追踪的链式赋值。对生产管道,把列名和类型作为可测试的数据契约。