Issue
I need to get sum of couple DataFrames and append each result to a target DataFrames(df_sum
)
df_sum = pd.DataFrame(columns = ['Source', 'Column2_SUM', 'Column3_SUM'])
I have 4 dataframe as
import pandas as pd
data_A = {'Column1': ['2023-06-16','2023-08-24','2023-04-24'],
'Column2': [4, 5, 6],
'Column3': [7, 8, 9]}
data_B = {'Column1': ['2023-07-19','2023-08-24','2023-03-18'],
'Column2': [4, 96, 6],
'Column3': [12, 8, 9]}
data_C = {'Column1': ['2023-06-22','2023-04-20','2023-09-12'],
'Column2': [4, 88, 6],
'Column3': [7, 8, 12]}
data_D = {'Column1': ['2023-08-27','2023-11-24','2023-04-08'],
'Column2': [4, 32, 6],
'Column3': [66, 8, 80]}
df_A = pd.DataFrame(data_A)
df_B = pd.DataFrame(data_B)
df_C = pd.DataFrame(data_C)
df_D = pd.DataFrame(data_D)
now what I need is to get something like, lo be loaded up to df_sum
Solution
Create dictionary for specify names of DataFrames, use concat
and aggregate sum
, add DataFrame.add_suffix
with DataFrame.rename_axis
and DataFrame.reset_index
for column SOURCE
:
d = {'df_A':df_A, 'df_B':df_B, 'df_C':df_C, 'df_D':df_D}
df = (pd.concat(d)
.groupby(level=0)[['Column2','Column3']].sum()
.add_suffix('_SUM')
.rename_axis('SOURCE')
.reset_index())
print (df)
SOURCE Column2_SUM Column3_SUM
0 df_A 15 24
1 df_B 106 29
2 df_C 98 27
3 df_D 42 154
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.