一个sheet 里有一列字段是地区,一列字段是年份,剩余的n列字段都是各种指标,怎么提取特定地区特定年份的特定指标数据并存到新excel?

sheet如下,工作表名称是'Sheet1',工作簿名称是'各地级市全要素原始数据.xlsx'。

程序如下:

import pandas as pd
import openpyxl as op
cities=['北京市','上海市','广州市','深圳市']
years=[i for i in range(2003,2019)]
file='各地级市全要素原始数据.xlsx'
ws=pd.read_excel(file)
city=list(ws['城市'])
year=list(ws['时间'])
wb=op.open(file)
ws=wb['Sheet1']
wb1=op.Workbook()
for sheet in range(8):#数字为指标个数
    ws1=wb1.create_sheet(ws.cell(1,4+sheet).value)
    for i in range(len(cities)):
        ws1.cell(1,i+2).value=cities[i]
    for i in range(len(years)):
        ws1.cell(i+2,1).value=years[i]
    for i in range(len(cities)):
        for j in range(len(years)):
            for k in range(len(city)):
                if city[k]==cities[i] and year[k]==years[j]:
                    ws1.cell(j+2,i+2).value=ws.cell(k+2,sheet+4).value
wb1.remove_sheet(wb1['Sheet'])
wb1.save('一线城市全要素原始数据.xlsx')

运行结果:

 

Sure! Here's an updated VBA code that generates different sheets based on the values in the last column (column with the header "Supplier"): ```vba Sub GenerateSheets() Dim ws As Worksheet Dim newWs As Worksheet Dim lastRow As Long Dim cellValue As Variant Dim supplierCol As Range ' Set the worksheet and range for the supplier column Set ws = ThisWorkbook.ActiveSheet lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row Set supplierCol = ws.Range("A1:A" & lastRow) ' Create a dictionary to store unique supplier names Dim supplierDict As Object Set supplierDict = CreateObject("Scripting.Dictionary") ' Loop through the supplier column and add unique names to the dictionary For Each cell In supplierCol cellValue = cell.Value If Not supplierDict.exists(cellValue) Then supplierDict.Add cellValue, True End If Next cell ' Create a new sheet for each unique supplier name For Each key In supplierDict.keys Set newWs = ThisWorkbook.Worksheets.Add newWs.Name = key ' Filter the data based on the current supplier and copy to the new sheet ws.AutoFilterMode = False supplierCol.AutoFilter Field:=1, Criteria1:=key ws.UsedRange.SpecialCells(xlCellTypeVisible).Copy newWs.Range("A1") ws.AutoFilterMode = False ' Adjust the column widths in the new sheet if necessary newWs.Columns.AutoFit Next key ' Show a message box when finished MsgBox "Sheets generated based on suppliers!" End Sub ``` You can copy this code into a VBA module in the Excel VBA editor (press `Alt+F11` to open the VBA editor). Make sure you have saved your workbook before running this macro, as it will make changes to the workbook. Run the macro, and it will create separate sheets for each unique supplier in the last column of the active sheet. The data related to each supplier will be filtered and copied to the respective sheet. Note: This code assumes that the last column in your worksheet is the "Supplier" column, and it uses the active sheet as the source. Please adjust the code accordingly if your requirements are different.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值