将列名的 dtype 从 float 更改为 string?

Change column names' dtype from float to string?

我最近在尝试将我的数据放入 tensorflow 格式时遇到此错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: column_name: age vocabulary dtype must be string or integer. dtype: <dtype: 'float64'>. 

是否有允许我将数据框列名的格式从浮点数更改为字符串的命令?

谢谢!

NLP(自然语言处理)包中,您可以使用 as.String(x) 将列名强制转换为字符串,然后将它们再次存储为数据框的列名。这可以迭代完成:

library(NLP)

float_colnames <- colnames(df)

string_colnames <- rep("", length(float_colnames))
for (i in 1:length(float_colnames)) {
  string_colnames[i] <- as.String(test[i])
}

colnames(df) <- string_colnames

希望对您有所帮助!