简体   繁体   中英

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. 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.

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. And since there are multiple sheets in the two excel files, the operation needs to be carried out via parallel loops for each sheets. Each respective sheets from both the excel files would generate a stacked plot.

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.

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.

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. The resultant dictionaries will have keys that are the same. pandas.read_excel

For example an excel file with three sheets named 'foo' , 'bar' , 'baz' .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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