Pandas 在循环中创建新的数据帧,并将结果附加到现有数据帧

Pandas 在循环中创建新的数据帧,并将结果附加到现有数据帧,pandas,numpy,for-loop,Pandas,Numpy,For Loop,我试图从数据帧中创建行和列的条件子集,并将它们附加到与子集结构匹配的现有数据帧中。新的数据子集需要存储在较小的数据帧中,这些较小数据帧的名称需要是动态的。下面是一个例子 #Sample Data df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [4,5,6,4,3,4,6,], 'c': [1,2,2,4,2,1,7], 'd': [4,4,2,2,3,5,6,], 'e': [1,3,3,4,2,1,7], 'f': [1,1,2,2,1,

我试图从数据帧中创建行和列的条件子集,并将它们附加到与子集结构匹配的现有数据帧中。新的数据子集需要存储在较小的数据帧中,这些较小数据帧的名称需要是动态的。下面是一个例子

#Sample Data

    df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [4,5,6,4,3,4,6,], 'c': [1,2,2,4,2,1,7], 'd': [4,4,2,2,3,5,6,], 'e': [1,3,3,4,2,1,7], 'f': [1,1,2,2,1,5,6,]})

#Function to apply to create the subsets of data - I would need to apply a #function like this to many combinations of columns

    def f1 (df, input_col1, input_col2):
        #Subset ros
        t=df[df[input_col1]>=3]
    #Subset of columns
        t=t[[input_col1, input_col2]]
        t = t.sort_values([input_col1], ascending=False)
        return t

#I want to create 3 different dataframes t1, #t2, and t3, but I would like to create them in the loop - not via individual #function calls.  
#These Individual calls - these are just examples of what I am trying to achieve via loop
#t1=f1(df, 'a', 'b')
#t2=f1(df, 'c', 'd')
#t3=f1(df, 'e', 'f')

#These are empty dataframes to which I would like to append the resulting #subsets of data

    column_names=['col1','col2']
    g1 = pd.DataFrame(np.empty(0, dtype=[('col1', 'f8'),('col2', 'f8')]))
    g2 = pd.DataFrame(np.empty(0, dtype=[('col1', 'f8'),('col2', 'f8')]))
    g3 = pd.DataFrame(np.empty(0, dtype=[('col1', 'f8'),('col2', 'f8')]))

    list1=['a', 'c', 'e']
    list2=['b', 'd', 'f']
    t={}
    g={}

#This is what I want in the end - I would like to call the function inside of #the loop, create new dataframes dynamically and then append them to the #existing dataframes, but I am getting errors.  Is it possible to do? 

    for c in range(1,4,1):
        for i,j in zip(list1,list2):
            t['t'+str(c)]=f1(df, i, j)
            g['g'+str(c)]=g['g'+str(c)].append(t['t'+str(c)], ignore_index=True)

我猜你想动态地创建t1,t2,t3

您可以使用
globals()


这正是我想要实现的。非常感谢。
g1 = pd.DataFrame(np.empty(0, dtype=[('a', 'f8'), ('b', 'f8')]))
g2 = pd.DataFrame(np.empty(0, dtype=[('c', 'f8'), ('d', 'f8')]))
g3 = pd.DataFrame(np.empty(0, dtype=[('e', 'f8'), ('f', 'f8')]))

list1 = ['a', 'c', 'e']
list2 = ['b', 'd', 'f']

for c in range(1, 4, 1):
    globals()['t' + str(c)] = f1(df, list1[c-1], list2[c-1])
    globals()['g' + str(c)] = globals()['g' + str(c)].append(globals()['t' + str(c)])