如何使用for循环和条件规则不规则地交错数据帧?

时间:2018-12-28 05:22:54

标签: python dataframe for-loop counter interleave

我试图根据每个数据帧中x1-x1和x2-x2之间的条件关系,交织(不规则地)两个数据帧(每个数据帧包含x1和x2的两列)。 使用单个for循环,并为每个数据帧使用一个计数器,我想根据条件(4x if和条件)将x1 / x2对增量添加到最终列表/数据帧中。 df_out是一个包含两个x1 / x2列和所有x1 / x2对的不规则拼接数据帧。 也许for循环中的双计数器有问题? (我的实际df包含30+列和1000的行...给出了df示例)

toolz交错不起作用,因为拼接不规则。 如果在if-conditional中缩进if-conditional不起作用,则迭代有时会失败,但不知道为什么。

dfDown输入数据帧1 dfDown input dataframe1

dfUp输入数据框2 dfUp input dataframe2

df_desired_out是必需的输出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

df_out应该看起来像这里显示的df_desired_out:

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

0 个答案:

没有答案