将列表中的列名分配给表 [英] Assigning column names from a list to a table
本文介绍了将列表中的列名分配给表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 100 行 x 25 列的数据表,没有列标题.我有一个包含 25 个项目的列表,我想将它分配为数据表的列标题(它们的顺序已经正确).我不知道如何使用熊猫有效地做到这一点.任何建议都会很棒!
谢谢.
解决方案
您可以直接分配给 columns
属性.
你也可以分配给 index 来完成类似的事情
<预><代码>>>>df.index = ["row1", "row2", "row3"]>>>打印文件a b c行 1 0 1 2行 2 0 1 2行 3 0 1 2I have, say, a 100 row by 25 column data table with no column headers. I have a 25-item list that I would like to assign as the column headers to the data table (they're in the right order already). I don't know how to do this efficiently using pandas. Any suggestions would be great!
Thanks.
解决方案
You can just assign to the columns
attribute directly.
>>> import pandas
>>> # create three rows of [0, 1, 2]
>>> df = pandas.DataFrame([range(3), range(3), range(3)])
>>> print df
0 1 2
0 0 1 2
1 0 1 2
2 0 1 2
>>> my_columns = ["a", "b", "c"]
>>> df.columns = my_columns
>>> print df
a b c
0 0 1 2
1 0 1 2
2 0 1 2
You can also assign to index to accomplish something similar
>>> df.index = ["row1", "row2", "row3"]
>>> print df
a b c
row1 0 1 2
row2 0 1 2
row3 0 1 2
这篇关于将列表中的列名分配给表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文