Pandas DataFrame merge summing column
In [41]: pd.merge(df1, df2, on=['id', 'name']).set_index(['id', 'name']).sum(axis=1)
Out[41]:
id name
2 B 25
3 C 20
dtype: int64
how to merge two dataframes and sum the values of columns
I think need set_index
for both DataFrame
s, add
and last reset_index
:
df = df1.set_index('Name').add(df2.set_index('Name'), fill_value=0).reset_index()
print (df)
Name class value
0 Ram 2.0 8.0
1 Sri 2.0 10.0
2 viv 7.0 8.0
If values in Name
are not unique use groupby
and aggregate sum
:
df = df1.groupby('Name').sum().add(df2.groupby('Name').sum(), fill_value=0).reset_index()
Merge data frames and sum columns with the same name
One way would be:
library(dplyr)
bind_rows(df1, df2) %>%
#mutate_if(is.numeric, tidyr::replace_na, 0) %>% #in case of having NAs
group_by(country) %>%
summarise_all(., sum, na.rm = TRUE)
# # A tibble: 4 x 3
# country year1 year2
# <chr> <dbl> <dbl>
# 1 a 2 2
# 2 b 4 4
# 3 c 3 3
# 4 d 3 3
or a base r solution
aggregate(. ~ country, rbind(df1, df2), sum, na.rm = TRUE, na.action = NULL)
which would generate the same output.
How to merge and sum two data frames
With dplyr,
library(dplyr)
# add rownames as a column in each data.frame and bind rows
bind_rows(df1 %>% add_rownames(),
df2 %>% add_rownames()) %>%
# evaluate following calls for each value in the rowname column
group_by(rowname) %>%
# add all non-grouping variables
summarise_all(sum)
## # A tibble: 7 x 4
## rowname x y z
## <chr> <int> <int> <int>
## 1 A 1 2 3
## 2 B 2 3 4
## 3 C 4 6 8
## 4 D 6 8 10
## 5 E 8 10 12
## 6 F 4 5 6
## 7 G 5 6 7
Pandas- merging two dataframe by sum the values of columns and index
You can use :df1.add(df2, fill_value=0)
. It will add df2
into df1
also it will replace NAN
value with 0
.
>>> import numpy as np
>>> import pandas as pd
>>> df2 = pd.DataFrame([(10,9),(8,4),(7,np.nan)], columns=['a','b'])
>>> df1 = pd.DataFrame([(1,2),(3,4),(5,6)], columns=['a','b'])
>>> df1.add(df2, fill_value=0)
a b
0 11 11.0
1 11 8.0
2 12 6.0
Pandas Merge and Sum Data Frames
To join dataframes in pandas
use pd.merge
. In the given case join is applied on columns with similar names, thus it's enough to pass the list of those column names as on
parameter:
merged = pd.merge(df_1, df_2, on=["ID1", "ID2"], how="left").fillna(0)
Next, calculate necessary columns using, for example, df.assign
:
merged = merged.assign(
VAL1 = lambda x: x.VAL1_x + x.VAL1_y,
VAL2 = lambda x: x.VAL2_x + x.VAL2_y)
Result:
columns = df_1.columns
merged[columns]
>>> ID1 ID2 VAL1 VAL2
0 CAR RED 6.0 6.0
1 TRUCK RED 6.0 6.0
2 CAR BLUE 1.0 1.0
how to sum two dataframes python
Try this:
df = pd.concat([df1, df2]).groupby(['dt']).sum().reset_index()
print(df)
PS: This is ensure all datetimes to exist.
multiply and sum two columns in two dataframes in Python
You will can just use pandas abstractions for it.
result = df['col1'] * df['col3']
If then you want to get the sum of those result values you can just do:
sum(results)
Python Pandas Summing columns in different data frames which have same column names, same index values but not same same length of index
No need to merge, let's use pandas intrinsic data aligment with indexes:
df1.set_index("Buckets")\
.add(df2.set_index("Buckets"), fill_value=0)\
.reset_index()
Output:
Buckets EUR
0 20Y 200.0
1 25Y 200.0
2 30Y 200.0
3 35Y 200.0
Note: You can leave out the set_index
if Buckets is already in the index.
Do, df1.add(df2, fill_value=0)
Related Topics
Call by Reference in R (Using Function to Modify an Object)
Finding the Index Inside a Vector Satisfying a Condition
How to Pass Dynamic Column Names in Dplyr into Custom Function
Import Data into R with an Unknown Number of Columns
Reverse Datetime (Posixct Data) Axis in Ggplot
Assign Value to Group Based on Condition in Column
How to Set the Default Language of Date in R
R: How to Get the Week Number of the Month
Bigrams Instead of Single Words in Termdocument Matrix Using R and Rweka
Select Row with Most Recent Date by Group
Add a Box for the Na Values to the Ggplot Legend for a Continuous Map
Stop an R Program Without Error
Smaller Gap Between Two Legends in One Plot (E.G. Color and Size Scale)
Density2D Plot Using Another Variable for the Fill (Similar to Geom_Tile)
Ggplot2: Connecting Points in Polar Coordinates with a Straight Line 2