Mahmoud Al-Haroon
Mahmoud Al-Haroon

Reputation: 2449

How to read the first column with its values in excel as a columns names in pandas data frame

So first of all I have an Excel file with a lot of sheets, now there's some of sheets that look like this:

|    Date    | 11-12-2019  | 12-12-2019   | 13-12-2019 | 14-12-2019  | 15-12-2019   |
|:-----------|------------:|:------------:|:-----------|------------:|:------------:|
| Col_1      |    1111     |     2222     | 3333       |        4444 |     5555     |
| Col_2      |    1111     |     2222     | 3333       |        4444 |     5555     |
| Col_3      |    1111     |     2222     | 3333       |        4444 |     5555     |
| Col_4      |    1111     |     2222     | 3333       |        4444 |     5555     |
| Col_5      |    1111     |     2222     | 3333       |        4444 |     5555     |
| Col_6      |    1111     |     2222     | 3333       |        4444 |     5555     |

I want to make it with pandas data frame to be something like this:

|    Date    |    Col_1    |     Col_2    | Col_3      |   Col_4     |     Col_5    |     Col_5    |
|:-----------|------------:|:------------:|:-----------|------------:|:------------:|:------------:|
| 11-12-2019 |    1111     |     1111     | 1111       |    1111     |     1111     |     1111     |
| 12-12-2019 |    2222     |     2222     | 2222       |    2222     |     2222     |     2222     |
| 13-12-2019 |    3333     |     3333     | 3333       |    3333     |     3333     |     3333     |
| 14-12-2019 |    4444     |     4444     | 4444       |    4444     |     4444     |     4444     |
| 15-12-2019 |    5555     |     5555     | 5555       |    5555     |     5555     |     5555     |

So is it possible to do this with python pandas or any other libraries?

Upvotes: 2

Views: 193

Answers (3)

typ993
typ993

Reputation: 11

You could potentially do this with the Python library/Excel add-in xlwings. Code would be something like this:

import xlwings as xw
sht = xw.Book().sheets[0]
pd_df = sht.range('reference to cells with data').options(transpose = True, pd.DataFrame, index = True, header = True).value

Upvotes: 1

jxc
jxc

Reputation: 13998

you can also use df.swapaxes

>>> df.set_index('Date').swapaxes(1,0)

Date        Col_1  Col_2  Col_3  Col_4  Col_5  Col_6
11-12-2019   1111   1111   1111   1111   1111   1111
12-12-2019   2222   2222   2222   2222   2222   2222
13-12-2019   3333   3333   3333   3333   3333   3333
14-12-2019   4444   4444   4444   4444   4444   4444
15-12-2019   5555   5555   5555   5555   5555   5555

Upvotes: 2

Erfan
Erfan

Reputation: 42916

We can do this using Transpose (same as DataFrame.T). Then replacing the column names by first row using DataFrame.iloc:

dft = df.T                  # transpose dataframe
dft.columns = dft.iloc[0]   # replace columns, by values in first row
dft = dft.iloc[1:]          # remove first row.
Date       Col_1 Col_2 Col_3 Col_4 Col_5 Col_6
11-12-2019  1111  1111  1111  1111  1111  1111
12-12-2019  2222  2222  2222  2222  2222  2222
13-12-2019  3333  3333  3333  3333  3333  3333
14-12-2019  4444  4444  4444  4444  4444  4444
15-12-2019  5555  5555  5555  5555  5555  5555

Upvotes: 3

Related Questions