简体   繁体   English

将列名称分配给csv数据集

[英]Assign column names to a csv dataset

I'm currently working on a dataset that consists of the following data: 我目前正在处理包含以下数据的数据集:

paper_id, word_attributes, class_label paper_id,word_attributes,class_label

Now there are a total of 3700 word_attributes columns representing a binary value. 现在总共有3700个word_attributes列表示一个二进制值。 The problem is that the column headers have not been assigned to the dataset. 问题在于列标题尚未分配给数据集。 So how do I go about assigning 3700+ column names in the .csv file? 那么,如何在.csv文件中分配3700+列名呢? Any suggestons? 有什么建议吗?

Thanks. 谢谢。

Edit: The .csv file is as follows; 编辑:.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
..............................
..............................

How do you store the header names? 您如何存储标题名称?

I would use CSV module in Python ( https://docs.python.org/2/library/csv.html ) but if you already have all header names listed then you can use "join" and just append the row on the top of the file. 我会在Python( https://docs.python.org/2/library/csv.html )中使用CSV模块,但是如果您已经列出了所有标头名称,则可以使用“ 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM