简体   繁体   English

在 python 中与连接的输出并行运行 2 个循环

[英]Running 2 loops in parallel with connected output in python

I have two excel files with inter-related data sets with equal number of rows, cols and sheets.我有两个 excel 文件,其中包含具有相同行数、列数和工作表数的相互关联的数据集。 I need to run two loops to read sheets, in default order, of both the excel files and then merge the finding of both the sheets to create stacked plots.我需要运行两个循环以默认顺序读取两个 excel 文件的工作表,然后合并两个工作表的发现以创建堆叠图。

More elaborately: The purpose is to read, say sheet1, in both the excel sheets and then carry out interpolation of the raw data to create stacked plots involving contour plots from data of respective sheet and co-relation line plot using data from both the sheets.更详细地说:目的是在两个 Excel 表中读取 sheet1,然后对原始数据进行插值,以创建包含来自各个表的数据的等高线图和使用来自两个表的数据的相关线图的堆叠图. And since there are multiple sheets in the two excel files, the operation needs to be carried out via parallel loops for each sheets.并且由于两个excel文件中有多个sheet,所以需要对每张sheet并行循环进行操作。 Each respective sheets from both the excel files would generate a stacked plot.来自两个 excel 文件的每个工作表都会生成一个堆叠图。

I have tried the following but could not succeed:我尝试了以下但无法成功:

import pandas as pd
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt

loc1 = r'E:\Python\Field_2\80_IP18p2.xlsx'
load1 = pd.read_excel(loc1,sheet_name = None, header=None)

loc2 = r'E:\Python\Field_2\80_OP18p2.xlsx'
load2 = pd.read_excel(loc2,sheet_name = None, header=None)

nar=list(load.keys())
numR = len(nar)

#since numR is same for both

while numR in ax:
    
    #first loop
    for k in load1:
        dem=load1[k]
        x_c=np.array(dem[0])
        y_c=np.array(dem[1])
        z_c=np.array(dem[2])

        X,Y=np.meshgrid(x_c,y_c)
        rbf = scipy.interpolate.Rbf(x_c, y_c, z_c, function='linear')
        Z=rbf(X,Y)
        
    #second loop
    for m in load2:
        demo=load2[m]
        x_co=np.array(demo[0])
        y_co=np.array(demo[1])
        z_co=np.array(demo[2])

        Xo,Yo=np.meshgrid(x_co,y_co)
        rbf = scipy.interpolate.Rbf(x_co, y_co, z_co, function='linear')
        Zo=rbf(X,Y)
    
    
    fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True, 
                         figsize=(12*3,4*3))
    #first stacked plot 
    cline=ax[0].tricontour(X,Y,Z, color='k')
    ax[0].clabel(cline, colors = 'k', fmt = '%2.1f', fontsize=7)       
    con=ax[0].tricontourf(X,Y,Z, cmap='rainbow',extend='both')
    plt.colorbar(con, shrink=.5)
    ax[0].set_aspect('equal')
   
    #second stacked plot
    clineo=ax[1].tricontour(Xo,Yo,Zo, color='k')
    ax[1].clabel(clineo, colors = 'k', fmt = '%2.1f', fontsize=7)       
    cono=ax[1].tricontourf(Xo,Yo,Zo, cmap='rainbow',extend='both')
    plt.colorbar(cono, shrink=.5)
    ax[1].set_aspect('equal')
    
    #third stacked plot
    ax[2].plot(x_c,y_c)
    ax[2].plot(x_co,y_co)

The above code creates blank plot with three stacked boxes.上面的代码创建了带有三个堆叠框的空白图。

Any help in creating the parallel loop as desired would be immensely helpful to me and shall be highly appreciated.根据需要创建并行循环的任何帮助对我都非常有帮助,并将受到高度赞赏。 And, I'd like to mention that I'm very new to python and so may have done some silly errors in the above attempt.而且,我想提一下,我对 python 非常陌生,因此在上述尝试中可能犯了一些愚蠢的错误。

Loop through two dictionaries in parallel.并行循环遍历两个字典。


I cannot find a suitable duplicate for your question.我找不到适合您问题的副本。 Assuming both dictionaries have the same keys, eg {'sheet1':sheet1_dataframe} - use the keys from one dictionary to access the values in the other dictionary.假设两个字典具有相同的键,例如{'sheet1':sheet1_dataframe} - 使用一个字典中的键来访问另一个字典中的值。

for key, df1 in load1.items():
    df2 = load2[key]
    # do all your processing

If the excel sheet names are different between the two files but you want to pair the first sheet of one with the first sheet of the other then specify a list of integers for the sheet names when reading the file.如果两个文件之间的 Excel 工作表名称不同,但您希望将一个的第一张工作表与另一个的第一张工作表配对,则在读取文件时指定工作表名称的整数列表。 The resultant dictionaries will have keys that are the same.生成的字典将具有相同的键。 pandas.read_excel pandas.read_excel

For example an excel file with three sheets named 'foo' , 'bar' , 'baz' .例如,一个包含三个名为'foo''bar''baz'表的 excel 文件。

In [106]: q = pd.read_excel('book1.xlsx', sheetname=[0,1,2])

In [107]: for key,value in q.items():
     ...:     print(f'key:{key}')
     ...:     print(value)
     ...:     print('.......')
     ...:     
key:0
   a  b
0  1  2
1  1  2
.......
key:1
   a  b
0  1  2
1  2  2
2  3  2
.......
key:2
   a  b  c
0  1  2  3
1  6  5  4
.......

In newer versions of Pandas the parameter is sheet_name instead of sheetname .在较新版本的 Pandas 中,参数是sheet_name而不是sheetname

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

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