Skip to content
Advertisement

Combine multiple dataframes wit pandas

I use the following script to measure the average RGB color of the picture in a selected path. I tried to make 1 dataframe with pd.concat but it doesn’t work out.

def Innovator(ImagePath, SavePath):
        for file in glob.glob(ImagePath):
            print(file)
            img = cv.imread(file)
            scale_percent = 60
            width = int(img.shape[1] * scale_percent / 100)
            height = int(img.shape[0] * scale_percent / 100)
            dim = (width, height)
            imgr = cv.resize(img, dim, interpolation=cv.INTER_AREA)
    
            hsv = cv.cvtColor(imgr, cv.COLOR_BGR2HSV)
            blur0 = cv.medianBlur(hsv, 11)
    
            low_yellow = np.array([10, 42, 210])
            high_yellow = np.array([30, 255, 255])
    
            mask = cv.inRange(blur0, low_yellow, high_yellow)
            res = cv.bitwise_and(imgr, imgr, mask=mask)
    
            fname = os.path.splitext(os.path.basename(file))[0]
    
            # print(fname)
    
            Imagefolder = str(SavePath) + '\' + 'Gesegmenteerde afbeelding'
    
            if not os.path.exists(Imagefolder):
                os.makedirs(Imagefolder)
    
            cv.imwrite(str(SavePath) + f'/Gesegmenteerde afbeelding/{fname}.jpg', res)
    
            result_df = pd.DataFrame()
            
                      
            #print(file)
            x = res[np.all(res != 0, axis=2)].mean(axis=0)
              
            x = pd.DataFrame(x)
            #print(x)
    
            x = pd.concat((result_df, pd.DataFrame(x)), axis=0)
    
            df_t = x.T
    
            df_t = pd.concat((result_df, pd.DataFrame(df_t)), axis=1)
            df_t.columns = ['R', 'G', 'B']
            df_t.loc['Mean'] = df_t.mean()
            df_t = df_t.round(decimals=1)

            print(df_t)

This is the result that I get: enter image description here

But I want just 1 dataframe with one average like this:

    R      G     B
      132   206,2 242,5
      134,2 208,6 243,6
      127,1 208,5 246,2
      137,6 205,8 240,5
Mean:

Advertisement

Answer

Use:

def Innovator(ImagePath, SavePath):
    #create list of DataFrames
    dfs = []
    for file in glob.glob(ImagePath):
        print(file)
        ...
        #for avoid duplicated rows
        #remove df_t.loc['Mean'] = df_t.mean()

        print (df_t)
        #append to list
        dfs.append(df_t)
    #outside for loops join together
    df_big = pd.concat(dfs)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement