1

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

1 Answer 1

1

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

10 Comments

This works great. I've just had another idea, can I do this to 2 dataframes as well?
you gave me this yesterday: all_df=[Price,RSP] Price, RSP = [d.reindex(forindex) for d in all_df]
Just trying to figure out how to apply this in this situation
So need dictionary of DataFrames in output?
@fred.schwartz - Added solution, please check it.
|

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.