首页 > 解决方案 > Python - 以所需格式转置数据

问题描述

Python新手在这里。请问我怎样才能将下面给定的数据转换成下面提到的所需格式?

源数据:

源数据

输出数据必须如下所示:

输出数据

请指教。

到目前为止我的代码:

# Transposing an excel file using python
import pandas as pd

# Location of the file
loc = ("C:\\Users\\user1\\Documents\\Python_Files\\data.xlsx")

# Reading the file
df=pd.read_excel("C:\\Users\\user1\\Documents\\Python_Files\\data.xlsx")

# Transposing the file
df.T.to_excel('C:\\Users\\user1\\Documents\\Python_Files\\data1.xlsx', index=False)

标签: pythonexceltranspose

解决方案


pandas您可以尝试使用带有函数pandas.read_csv read_csv 文档的库将原始数据作为数据帧导入,然后使用函数pandas.DataFrame.transpose transpose 文档转置数据帧


推荐阅读