Python pandas, How can I read an excel file without a column label and then insert a column label?

I have lists that I want to insert as column labels. But when I use read_excel from pandas, they always treat the 0th row as the column label. How could I read the file as a pandas dataframe and then put it as a 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 

Here is my personal list for the column name

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

And I want to make the dataframe 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 
+6
source share
1 answer

Pass header=None to say that there is no header, and you can pass the list to names to tell it what you want to use at the same time. (Note that your example is missing a column name, I assume this is random.)

For instance:

 >>> 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 df.columns .

+12
source

Source: https://habr.com/ru/post/972112/


All Articles