Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pandas

excel

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  

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
like image 454
JonghoKim Avatar asked Jul 12 '14 02:07

JonghoKim


People also ask

How do I read a CSV file without columns in Python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.

How do you skip columns while reading Excel in pandas?

Skip Columns From Excel Sheet Sometimes while reading an excel sheet into pandas DataFrame you may need to skip columns, you can do this by using usecols param. This takes values {int, str, list-like, or callable default None}. To specify the list of column names or positions use a list of strings or a list of int.

How do you read data from Excel file in Python using pandas?

To read an excel file as a DataFrame, use the pandas read_excel() method. You can read the first sheet, specific sheets, multiple sheets or all sheets. Pandas converts this to the DataFrame structure, which is a tabular like structure.

How read specific rows and columns from pandas Excel?

To tell pandas to start reading an Excel sheet from a specific row, use the argument header = 0-indexed row where to start reading. By default, header=0, and the first such row is used to give the names of the data frame columns. To skip rows at the end of a sheet, use skipfooter = number of rows to skip.


1 Answers

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

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.

like image 112
DSM Avatar answered Nov 16 '22 02:11

DSM