0

I am trying to interleave (irregularly) two dataframes (each containing two columns of x1’s and x2’s), dependant upon the conditional relationship between the x1-x1 and x2-x2 in each dataframe. Using a single for loop, and a counter for each dataframe, I want to incrementally add an x1/x2 pair into the final list/dataframe, dependant upon the conditionals (4x if-and conditionals). The df_out is a single irregularly-spliced dataframe containing two columns, x1/x2, with all of the x1/x2 pairs. Perhaps problem with dual-counters in a for-loop? (my actual df contains 30+ columns and 1000's rows...example df given)

toolz interleave does not work as the splicing is irregular. if-conditional indented within if-conditional does not work, iteration fails at some point, but do not know why.

dfDown input dataframe1 dfDown input dataframe1

dfUp input dataframe2 dfUp input dataframe2

df_desired_out is the required output df df_desired_out is the required output df

import pandas as pd
import numpy as np

dataDown = {'x1':(0,0,0,0,0,0,0), 'x2':(2,10,20,25,33,47,57)}
dataUp = {'x1':(2,2,2,2,2,2), 'x2':(7,13,24,30,36,39)}

dfDown = pd.DataFrame(dataDown)
dfUp = pd.DataFrame(dataUp)

totalUpDown = len(dfUp) + len(dfDown)   # total number of x1/x2 pairs
countUpDown = np.arange(totalUpDown)   # to be used in for loop
allUpDown = []   # empty list
countUp = 0   # up data counter to be used in for loop
countDown = 0   # down data counter to be used in for loop

for count in countUpDown:   # single for loop containing 4 exclusive conditionals, and two 'counters'

    #   this conditional should write a dfDown x1/x2 pair into list allUpDown, and increment down-counter by 1
    if dfDown['x1'][countDown] < dfUp['x1'][countUp] and dfDown['x2'][countDown] < dfUp['x2'][countUp]:
        combi = pd.DataFrame([[[dfDown['x1'][countDown]], dfDown['x2'][countDown]]],
                             columns = ['x1', 'x2'])
        allUpDown.append(combi)
        countDown +=1

    #   this conditional should write a dfUp x1/x2 pair into list allUpDown, and increment up-counter by 1 
    if dfDown['x1'][countDown] < dfUp['x1'][countUp] and dfDown['x2'][countDown] > dfUp['x2'][countUp]:
        combi = pd.DataFrame([[[dfUp['x1'][countUp]], dfUp['x2'][countUp]]],
                             columns = ['x1', 'x2'])
        allUpDown.append(combi)
        countUp +=1

    #   this conditional should write a dfDown x1/x2 pair into list allUpDown, and increment down-counter by 1 
    if dfDown['x1'][countDown] > dfUp['x1'][countUp] and dfDown['x2'][countDown] < dfUp['x2'][countUp]:
        combi = pd.DataFrame([[[dfDown['x1'][countDown]], dfDown['x2'][countDown]]],
                             columns = ['x1', 'x2'])
        allUpDown.append(combi)
        countDown +=1

    #   this conditional should write a dfUp x1/x2 pair into list allUpDown, and increment up-counter by 1 
    if dfDown['x1'][countDown] > dfUp['x1'][countUp] and dfDown['x2'][countDown] > dfUp['x2'][countUp]:
        combi = pd.DataFrame([[[dfUp['x1'][countUp]], dfUp['x2'][countUp]]],
                             columns = ['x1', 'x2'])
        allUpDown.append(combi)
        countUp +=1

# Build the interleaved dataframe from the list of all x1/x2 pairs
df_out = pd.concat(allUpDown, ignore_index = True)    
df_out

The df_out should look like the df_desired_out shown here:

desired_out = {'x1':(0,2,0,2,0,2,0,2,0,2,2,0,0), 'x2':(2,7,10,13,20,24,25,30,33,36,39,47,57)}
df_desired_out = pd.DataFrame(desired_out)
df_desired_out
MarkD
  • 395
  • 3
  • 14

0 Answers0