fred.schwartz
fred.schwartz

Reputation: 2155

Adding Columns in loop pandas

I have a 2 dataframes each with 2 columns (named the same in both df's) and I want to add them together to make a third column.

df1['C']=df1[['A','B']].sum(axis=1) 
 df1['D']=df1[['E','G']].sum(axis=1)
df2['C']=df2[['A','B']].sum(axis=1) 
 df2['D']=df2[['E','G']].sum(axis=1)

However in reality its more complicated than this. So can I put these in a dictionary and loop?

I'm still figuring out how to structure dictionarys for this type of problem, so any advice would be great.

Here's what I'm trying to do:

all_dfs=[df1,df2]

for df in all_dfs:

dict={Out=['C'], in=['A','B]
    Out2=['D'], in2=['E','G]
}

for i in dict:
    df[i]=df[['i[1....

I'm a bit lost in how to build this last bit

Upvotes: 1

Views: 54

Answers (1)

jezrael
jezrael

Reputation: 862511

First change dictionary name because dict is python code word, then change it by key with output column and value by list of input columns and last loop by items() method:

d= {'C':['A','B'],'D': ['E','G']}

for k, v in d.items():
    #checking key and value of dict
    print (k)
    print (v) 
    df[k]=df[v].sum(axis=1)

EDIT:

Here is simplier working with dictionary of DataFrames, use sum and last create anoter dictionary of DataFrames:

all_dfs= {'first': df1, 'second':df2}
out = {}

for name, df in all_dfs.items():
    d= {'C':['A','B'],'D': ['E','G']}
    for k, v in d.items():
        df[k]=df[v].sum(axis=1)
        #fill empty dict by name
        out[name] = df

print (out)
print (out['first'])
print (out['second'])

Upvotes: 1

Related Questions