为CSV数据集分配列名

0 投票
1 回答
529 浏览
提问于 2025-04-18 04:22

我现在正在处理一个数据集,里面包含以下数据:

paper_id, word_attributes, class_label

现在总共有3700个word_attributes列,每个列代表一个二进制值。问题是,这些列的标题还没有分配给数据集。那么,我该如何给这个.csv文件里的3700多个列命名呢?有什么建议吗?

谢谢。

编辑:

这个.csv文件的内容如下:

100157,0,0,0,0,0,0,0,0,0,0,0,0,.....,Agents
100598,0,1,0,0,0,0,0,0,0,0,0,0,.....,IR
..............................
..............................

1 个回答

0

你是怎么存储表头名称的?

我会在Python中使用CSV模块(https://docs.python.org/2/library/csv.html),不过如果你已经有了所有的表头名称列表,那你可以用“join”这个方法,把它们合并起来,然后把这一行加到文件的最上面。

header_row = header_name_list.join(",")

file_to_read = open("your file path" , "r") 
old_content = file_to_read.read()
file_to_read.close()

content_to_write = "%s\n%s" % (header_row, old_content)
file_to_write = open("your file path" , "w")
file_to_write.write(content_to_write)
file_to_write.flush()
file_to_write.close()

撰写回答