简体   繁体   English

如何使用熊猫按国家/地区列将数据拆分为一个工作簿中的多个工作表

[英]How to Split data into multiple sheets in one single workbook by country column using pandas

I am trying to pull data from a table and load into a excel workbook, i want to separate data by country and each country data will be loaded into each sheet with country name.我正在尝试从表中提取数据并加载到 excel 工作簿中,我想按国家/地区分隔数据,并且每个国家/地区数据将加载到带有国家/地区名称的每个工作表中。 But unfortunately i am didnt find exact logic to achieve this scenario.但不幸的是,我没有找到实现这种情况的确切逻辑。 I need help here with errors and code to fix.Below is my code.我需要帮助解决错误和代码来修复。下面是我的代码。

import pandas as pd

with pd.ExcelWriter('meddevices_tm.xlsx') as writer:
    dftm = pd.read_excel('')
    dbconnect = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server=digital-dbserver.database.windows.net;Database=gdmd-db;uid=adm;pwd=gr!")
    tsql= 'select [Franchise Name],[Prod Number],[Product Description],[Net Content],[Net Content UOM],[Product Type],[Base Unit Indicator],[Dispatch Unit Indicator],[Invoice Unit Indicator],[Ordering Unit Indicator],[Country] from [dbo].[onews_med_devices] '
    cursor= dbconnect.cursor()
    dftm =pd.read_sql(tsql,dbconnect)
    dftm = pd.read_excel('Med Devices0818.xlsx')
    split_values = dftm['Country'].sort_values().unique()
    
    print(split_values)
    for value in split_values:
         print (value)
         (dftm.query(f'Country == {value}').drop(columns='Country').copy().to_excel(excel_writer=writer,sheet_name=f'Country{value}',index=False))

You can use groupby您可以使用groupby

for country, filtered_df in dftm.groupby('Country'):
    filtered_df.to_excel(...

This will return a filtered dataframe by each country, which you can save to a desired location.这将返回每个国家/地区的过滤数据框,您可以将其保存到所需位置。

暂无
暂无

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

相关问题 当第一列在工作簿中的名称不同时,如何使用 Python 在单个工作簿中合并多个工作表 - How to merge multiple sheets in a single workbook using Python when the first column is named differently across the workbook 如何根据不同的条件将数据写入多张单个工作簿? - How to write data into multiple sheets of single workbook based on different conditions? 如何使用正则表达式将一列拆分为Pandas中的多列? - How to split one column into multiple columns in Pandas using regular expression? 将单个工作簿中的Excel工作表将列值合并到Pandas数据框中 - Merging excel sheets in a single workbook on column value into a pandas dataframe 如何使用 python 将 excel 的一列中的数据拆分为多列 - How to Split data in one column of excel into multiple column using python 如何使用 pandas 数据框将数据框的每一列值添加到一张一张的新工作表中 - How to add each column of a data frame values in one by one new sheets using pandas data frame 如何使用 pandas 在单行列中添加多个数据 - How to add multiple data in single Row Column using pandas 使用 pandas 从同一工作簿中的多个 excel 工作表中提取部分数据 - Extract Partial Data from multiple excel sheets in the same workbook using pandas 如何使用 pandas 将多个 pivot 表插入工作簿的一个工作表 - How to insert multiple pivot tables into one worksheet of a workbook using pandas 如何将单个 Pandas Dataframe 列的内容拆分为多个新列 - How to Split the Contents of a Single Pandas Dataframe Column into Multiple New Columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM