Python pandas,如何读取没有列标签的excel文件然后插入列标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24709108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:14:13  来源:igfitidea点击:

Python pandas, How could I read excel file without column label and then insert column label?

excelpandas

提问by JonghoKim

I have lists which I want to insert it as column labels. But when I use read_excel of pandas, they always consider 0th row as column label. How could I read the file as pandas dataframe and then put the list as column label

我有我想将其作为列标签插入的列表。但是当我使用熊猫的 read_excel 时,他们总是将第 0 行视为列标签。我怎么能把文件读作熊猫数据框,然后把列表作为列标签

  orig_index = pd.read_excel(basic_info, sheetname = 'KI12E00')

    0.619159  0.264191  0.438849  0.465287  0.445819  0.412582  0.397366  \
0   0.601379  0.303953  0.457524  0.432335  0.415333  0.382093  0.382361   
1   0.579914  0.343715  0.418294  0.401129  0.385508  0.355392  0.355123  

Here is my personal list for column name

这是我个人的列名列表

   print set_index
[20140109, 20140213, 20140313, 20140410, 20140508, 20140612]

And I want to make dataframe as below

我想制作如下数据框

    20140109  20140213  20140313  20140410  20140508  20140612
0   0.619159  0.264191  0.438849  0.465287  0.445819  0.412582  0.397366  \
1   0.601379  0.303953  0.457524  0.432335  0.415333  0.382093  0.382361   
2   0.579914  0.343715  0.418294  0.401129  0.385508  0.355392  0.355123

回答by DSM

Pass header=Noneto tell it there isn't a header, and you can pass a list in namesto tell it what you want to use at the same time. (Note that you're missing a column name in your example; I'm assuming that's accidental.)

传递header=None告诉它没有标题,您可以同时传递一个列表names来告诉它您要使用的内容。(请注意,您的示例中缺少列名;我假设这是偶然的。)

For example:

例如:

>>> df = pd.read_excel("out.xlsx", header=None)
>>> df
          0         1         2         3         4         5         6
0  0.619159  0.264191  0.438849  0.465287  0.445819  0.412582  0.397366
1  0.601379  0.303953  0.457524  0.432335  0.415333  0.382093  0.382361
2  0.579914  0.343715  0.418294  0.401129  0.385508  0.355392  0.355123

or

或者

>>> names = [20140109, 20140213, 20140313, 20140410, 20140508, 20140612, 20140714]
>>> df = pd.read_excel("out.xlsx", header=None, names=names)
>>> df
   20140109  20140213  20140313  20140410  20140508  20140612  20140714
0  0.619159  0.264191  0.438849  0.465287  0.445819  0.412582  0.397366
1  0.601379  0.303953  0.457524  0.432335  0.415333  0.382093  0.382361
2  0.579914  0.343715  0.418294  0.401129  0.385508  0.355392  0.355123

And you can always set the column names after the fact by assigning to df.columns.

并且您始终可以通过分配给df.columns.