Python pandas ,如何读取没有列标签的Excel文件,然后插入列标签? [英] Python pandas, How could I read excel file without column label and then insert column label?

查看:231
本文介绍了Python pandas ,如何读取没有列标签的Excel文件,然后插入列标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些列表,希望将其作为列标签插入. 但是,当我使用pandas的read_excel时,他们总是将第0行视为列标签. 如何将文件读取为pandas数据框,然后将列表作为列标签

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

  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  

这是我个人的列名列表

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

我想制作如下数据框

    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

推荐答案

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

Pass header=None to tell it there isn't a header, and you can pass a list in names to 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.)

例如:

>>> 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

>>> 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

而且您始终可以在事后通过分配给df.columns来设置列名.

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

这篇关于Python pandas ,如何读取没有列标签的Excel文件,然后插入列标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆