如何处理合并的单元格并将行转换为列?

2024-04-19 19:04:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在excel文件中有这样的数据: enter image description here

我想格式化数据的结构:

我想要像这样的东西

enter image description here

您可以推荐哪些方法


Tags: 文件数据方法结构excel
3条回答

嗯,你可以试试这样的。没有实际的数据来测试它,我无法确认。这也许是一个坚实的起点

df = pd.read_excel(open('NAMEOFFILE.xlsx', 'rb')
df = df.transpose()
df.to_excel('NAMEOFFILE.xlsx', sheet_name='Transposed')

如果您只需要在excel中快速执行此操作:高亮显示所有数据,复制数据,然后右键单击将数据粘贴到您希望粘贴的位置,然后单击“粘贴特殊”,最后单击“转置” Click this one.

在Excel中:

  1. 手动取消合并合并单元格

  2. 复制范围

  3. 粘贴特殊转置

Link From Microsoft

Page from Microsoft

T将数据帧中的行转换为列

region=['Qtr1','Qtr2','Qtr3','Qtr4']
europe=[21704714,17987034,19485029,22567894]
asia=[8774099,12214447,14356879,15763492]
north_america=[12094215,10873099,15689543,17456723]

df=pd.DataFrame({'Region':region,'Europe':europe,'Asia':asia,'North    America':north_america})
types = pd.Series(data=['str', 'int', 'int', 'int'], index=['Region', 'Europe', 'Asia', 'North America'])
types = types.apply(eval)
df.reset_index()
df = df.astype(dtype=types.to_dict())
df=df.T
print(df.head())

使用 xls=pd.read\u excel(url,sheet\u name=None)将数据检索到数据框中

相关问题 更多 >