0

I am trying to create conditional subsets of rows and columns from a DataFrame and append them to the existing dataframes that match the structure of the subsets. New subsets of data would need to be stored in the smaller dataframes and names of these smaller dataframes would need to be dynamic. Below is an example.

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

1 Answer 1

1

I guess you want to create t1,t2,t3 dynamically.

You can use 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)])
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was trying to accomplish. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.