'How to add a column of a dictionary of dataframes?
I have a dictionary of 15 dataframes and I was searching around how to add a column to each dataframe based on an operation. In my particular case, I wanted to sum specific columns in each dataframe and have the result of that operation in a new column within each dataframe. Such as below.
df['Sum'] = df[columnlist].sum(axis=1)
In order to apply this to each dataframe in the dictionary I ended up with this solution:
def sumrows(df):
df['Sum'] = df[columnlist].sum(axis=1)
return df
sum = {}
sum = {k: sumrows(v) for k,v in dictionary.items()}
I am curious if there is a way to do this with out creating and calling a function within my dictionary comprehension. I didn't see many resources on how to do this so figured this might be a beneficial post for fellow beginners like myself.
Solution 1:[1]
If df_dict is your dictionary of dataframes and columnlist the list of columns you want to sum over, then you could use .assign() and do:
df_sum_dict = {
key: df.assign(Sum=df[columnlist].sum(axis=1)) for key, df in df_dict.items()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Timus |
