【问题标题】:New dataframe creation within loop and append of the results to the existing dataframe在循环内创建新数据框并将结果附加到现有数据框
【发布时间】:2023-03-17 09:09:01
【问题描述】:

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

#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)

【问题讨论】:

    标签: pandas numpy for-loop


    【解决方案1】:

    我猜你想动态创建 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)])
    

    【讨论】:

    • 这正是我想要完成的。谢谢!
    猜你喜欢
    • 2021-07-29
    • 2020-11-12
    • 2019-08-07
    • 2017-10-10
    • 2016-07-24
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2019-11-19
    相关资源
    最近更新 更多