gpt4 book ai didi

python - 在 python 中 reshape 多头表

转载 作者:行者123 更新时间:2023-12-04 01:08:21 26 4
gpt4 key购买 nike

我是 python 的新手,我试图从 excel 文件 reshape 表格,因为我有多个标题我试图将第一个标题转换为 2 个单独的列。我在这里附上我的代码输出和数据。 Input Table

import pandas as pd
import numpy as nm

df = pd.read_excel(r'.\test.xlsx', header=[0, 1])
df = (df.stack(0, dropna=False)
.rename_axis(index=('Customer','Date'), columns=None)
.reset_index())
df.to_csv(r'.\testnew.csv',index=False)
print(df)

打印输出 - enter image description here

期望的输出-

<表类="s-表"><头>客户日期预算实际金额<正文>约翰1 月 20 日100500约翰2 月 20 日约翰3 月 20 日克里斯1 月 20 日120800克里斯2 月 20 日501020克里斯3 月 20 日5045

最佳答案

我相信你需要DataFrame.stack :

df = pd.read_excel(r'.\test.xlsx', header=[0, 1])

df = (df.stack(0, dropna=False)
.rename_axis(index=('Customer','Date'), columns=None)
.reset_index())

关于python - 在 python 中 reshape 多头表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65576968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com