[英]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:所以首先我有一个包含很多工作表的 Excel 文件,现在有一些工作表看起来像这样:
| 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?那么是否可以使用 python pandas 或任何其他库来做到这一点?
We can do this using Transpose
(same as DataFrame.T
).我们可以使用Transpose
(与DataFrame.T
相同)来做到这一点。 Then replacing the column names by first row using DataFrame.iloc
:然后使用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
you can also use df.swapaxes你也可以使用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
You could potentially do this with the Python library/Excel add-in xlwings.您可以使用 Python 库/Excel 插件 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.