Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 858
Aurélien Pierre Avatar asked Sep 20 '26 14:09

Aurélien Pierre


1 Answers

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
like image 83
Psidom Avatar answered Sep 23 '26 03:09

Psidom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!