How to Merge Two Dataframes Side-By-Side

Join two data-frames side by side

Say I have the following dataframes;

df1=pd.DataFrame({'a':[1,2,3,4,5,6], 'b':[6,7,8,9,9,0],'c':[6,7,8,9,9,0],'d':[6,7,8,9,9,0],'e':[6,7,8,9,9,0]})
df2=pd.DataFrame({'a':[1,2,13,41,5,6], 'b':[61,7,83,9,9,60],'c':[61,7,83,9,9,60],'d':[61,7,83,9,9,60],'e':[61,7,83,9,9,60]})

Set a common index(You must be careful though, make sure you set an index common among the datframes.In this case, I just reset them to make it start from 0 on wards)

df1= df.iloc[0:3, 1:3]
df1.reset_index(drop=True, inplace=True)
df2=df.iloc[3:6, 1:3]
df2.reset_index(drop=True, inplace=True)

Do an inner concat

result = pd.concat([df1, df2], axis=1, join='inner')
result

Combining multiple dataframes side by side and renaming them

Use concat.

If Activity are indexes in both dataframes use:

df = pd.concat([df1, df2], axis=1, keys=('China Analysis','America Analysis'))

If not, first set_index:

df = pd.concat([df1.set_index('Activity'), 
df2.set_index('Activity')],
axis=1,
keys=('China Analysis','America Analysis'))
print (df)
China Analysis America Analysis
No of people No of people
Activity
Activity 1 100 400
Activity 2 200 500
Activity 3 300 500

Create index and merge two dataframes side by side after transpose

Assuming df1 is the name of the first table and transpose is the name of the transposed table, you can use:

transpose.join(df1.set_index(transpose.index))

# Result:

Metal Rock NumberofTracks NumberofAlbums
count 4 2 200 12

Here, we make use of .set_index() to set the index of df1 to the same index of dataframe transpose. Thus, enabling them to .join() together because of having same index.

Paste 2 data frames side by side without any key

If the number of rows are same use

cbind(df1, df2)
# A B E H A B
#1 x1 x2 x3 x6 y1 y2
#2 x1 x2 x4 x7 y1 y2
#3 x1 x2 x5 x8 y1 y2

Or in dplyr

library(dplyr)
library(stringr)
df2 %>%
rename_all(~ str_c(., ".1")) %>%
bind_cols(df1, .)

In some versions of dplyr (0.8.5), it would rename correctly when there are duplicate column names

bind_cols(df1, df2)

NOTE: It is not recommended to have same column names in data.frame so we could change the column names with make.unique


If we have two datasets with unequal number of rows

library(rowr)
cbind.fill(df1, df2new, fill = NA)
# A B E H A B
#1 x1 x2 x3 x6 y1 y2
#2 x1 x2 x4 x7 y1 y2
#3 x1 x2 x5 x8 <NA> <NA>

Or with base R

mxn <- max(nrow(df1), nrow(df2new))
df2new[(nrow(df2new)+1):mxn,] <- NA
cbind(df1, df2new)
# A B E H A B
#1 x1 x2 x3 x6 y1 y2
#2 x1 x2 x4 x7 y1 y2
#3 x1 x2 x5 x8 <NA> <NA>

data

df1 <- structure(list(A = c("x1", "x1", "x1"), B = c("x2", "x2", "x2"
), E = c("x3", "x4", "x5"), H = c("x6", "x7", "x8")),
class = "data.frame", row.names = c(NA,
-3L))

df2 <- structure(list(A = c("y1", "y1", "y1"), B = c("y2", "y2", "y2"
)), class = "data.frame", row.names = c(NA, -3L))

df2new <- structure(list(A = c("y1", "y1"), B = c("y2", "y2")), class = "data.frame", row.names = c(NA,
-2L))

how to append two columns in a dataframe side by side in pandas

You can slice df_final into two dataframes, and then you can concatenate both frames two one as we did here:

df_cost = df_final.iloc[:, 0:3]
df_total_cn = df_final.iloc[:, 3:6]
df = pd.concat([df_cost, df_total_cn], axis=1, keys=['cost', 'total cn']).swaplevel(axis=1).sort_index('columns').fillna(0)
print(df)

2022-01 2022-02 2022-03
cost total cn cost total cn cost total cn
location
AGH 2062.25 0 6561.43 0 3154.43 0
AIM 3122.74 0 3277.56 0 3631.45 0
AJL 1641.82 0 3229.49 0 4937.23 0


Related Topics



Leave a reply



Submit