excel多列拼接成三列python

img


所上图所标,有个excel,有很多列,每三列为一组,希望把4-6列加到1-3列后面
7-9列加到4-6列后面,依此类推,最后变成三列
想要的结构为
一二三
四五六
七八七
感谢

在Python中,可以使用Pandas库来处理Excel数据。假设你有一个Excel表格,其中有多列数据需要拼接成三列,可以按照以下步骤操作:

1.首先,导入Pandas库和Excel文件:

import pandas as pd

df = pd.read_excel('your_excel_file.xlsx')


2.然后,选择需要拼接的列,并使用concat函数进行拼接。注意,拼接前需要将每一列转换为字符串类型:

col1 = df['Column1'].astype(str)
col2 = df['Column2'].astype(str)
col3 = df['Column3'].astype(str)

result = pd.concat([col1, col2, col3], axis=1)

3.最后,将结果保存到一个新的Excel文件中:


result.to_excel('new_excel_file.xlsx', index=False)

完整的代码如下:

import pandas as pd

df = pd.read_excel('your_excel_file.xlsx')

col1 = df['Column1'].astype(str)
col2 = df['Column2'].astype(str)
col3 = df['Column3'].astype(str)

result = pd.concat([col1, col2, col3], axis=1)

result.to_excel('new_excel_file.xlsx', index=False)


不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^