简体   繁体   English

通过 Python 将数据合并到 Excel 中的 1 个工作表中

[英]Combine data into 1 worksheet in Excel via Python

I have a bunch of worksheets in single excel file in the same format.我在单个 excel 文件中有一堆相同格式的工作表。 I need to take just one column out of each table and to combine those data in new worksheet and new excel file.我只需要从每个表中取出一列,并将这些数据合并到新的工作表和新的 excel 文件中。

目标示例

I have struggled to find solution for my question.我一直在努力为我的问题找到解决方案。 But still I have not reached any results.但我仍然没有达到任何结果。

I will appreciate any help or link to helpful materials.我将不胜感激任何帮助或有用材料的链接。

You can use pd.read_excel() and iterate over each sheet in the workbook.您可以使用pd.read_excel()并遍历工作簿中的每个工作表。

import pandas as pd
data = pd.read_excel( YOUR_FILE, sheet_name=None )
ouput_dict = dict()
for i,sheet in enumerate( data.keys() ):
    temp_df = data[sheet]
    new_col_name = col + str(i)
    output_dict[ new_col_name ] = temp_df[ col ]
final_df = pd.DataFrame( data=output_dict )

Inside the for loop, temp_df will be a pandas DataFrame based on that individual sheet.for循环中, temp_df将是基于该单独工作表的 pandas DataFrame In this example you could name the column new_col_name , taking the column col from that sheet.在此示例中,您可以将列命名为new_col_name ,并从该工作表中获取列col In your example, col would be "Open".在您的示例中, col将是“打开”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM