I am looking for python code to paste value strings of two dataframes with identical column names, dimensions and datatype (Strings).
I want to create a new dataframe by pasting each element of DF1 & DF2 together which is in the corresponding location within each dataframe and have a "," delimiter between each string.
Input dataframes (DF1 & DF2) and expected output
Equivalent R code would be using matrices: matrix(paste(DF1,DF2, sep=";"),nrow = nrow(DF1), dimnames=dimnames(DF1))
Is there a way to do this with python? Appears like it should be simple but thanks in advance!
CodePudding user response:
Use pd.concat
:
out = pd.concat([df1.stack(), df2.stack()], axis=1) \
.apply(lambda x: ','.join(x), axis=1) \
.unstack()
Output:
>>> out
A B C
0 z,f x,h c,j
1 v,t b,y n,u
2 a,u s,i d,o
CodePudding user response:
You could add columns together directly, and use pd.concat
to collect them into a dataframe.
import pandas as pd
df1 = pd.DataFrame({"a": ["big", "small"], "b": ["good", "bad"]})
df2 = pd.DataFrame({"a": ["shrimp", "lobster"], "b": ["will", "omen"]})
pd.concat([df1[col] "," df2[col] for col in df1],
axis="columns")
# a b
# 0 big,shrimp good,will
# 1 small,lobster bad,omen
EDIT: Better yet, you can just add the dataframes in their entirety.
df1 "," df2
# a b
# 0 big,shrimp good,will
# 1 small,lobster bad,omen