Aurélien Pierre Aurélien Pierre - 1 month ago 17
Python Question

pandas + python : merge 2 dataframes cell by cell

I have two

pandas.DataFrame
:

values = pandas.DataFrame([[0, 1], [7,5]], columns=["a", "b"], index=[1, 2])
info = pandas.DataFrame([["foo", "bar"], ["few", "tar"]], columns=["a", "b"], index=[1, 2])


values
and
info
are settings for users so I want to print a chart by merging the dataframes cell by cell on all the columns to get something like this :

a b
1 0 : foo 1 : bar
2 7 : few 5 : tar


How can I do this without a for loop ?

Answer

You can convert values data type to str and add the two data frames with : as separator.

values.apply(lambda col: col.astype(str)) + " : " + info

#   a       b
#1  0 : foo 1 : bar
#2  7 : few 5 : tar
Comments