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

Julia:删除DataFrame中名称以数字开头的列报错及解决方法

Fixing UndefVarError When Deleting a DataFrame Column Starting with a Number in Julia

Let's break down why you're hitting that UndefVarError: SsnPorchH not defined error first:

When you write :3SsnPorchH in Julia, the language doesn't interpret this as a single symbol representing your column name. Instead, it parses it as the expression 3 * SsnPorchH—treating 3 as a number and SsnPorchH as an undefined variable, hence the error.

Luckily, there are a few straightforward ways to fix this and delete your target column correctly:

Method 1: Use a quoted symbol literal

Wrap the column name in double quotes immediately after the colon to force Julia to recognize it as a single symbol:

delete!(features, [:"3SsnPorchH"])

Method 2: Explicitly create a Symbol from a string

If you prefer working with strings (especially useful if your column name is generated dynamically), convert the string to a Symbol first:

target_col = Symbol("3SsnPorchH")
delete!(features, [target_col])

Method 3: Use the column name as a string (DataFrames v1.0+)

Starting with DataFrames version 1.0, most column-manipulation functions accept string inputs directly. This is often the simplest approach for special-named columns:

delete!(features, ["3SsnPorchH"])

All three methods will correctly target your 3SsnPorchH column without triggering parsing errors. This trick works for any column name with special characters, spaces, or leading numbers—just remember to avoid raw symbol literals for those cases!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:24:06