Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I combine two different row string values into a single new row value?

I have two rows of a dataframe likeso:

   Jan  Feb
a   1    3
b   2    4  

I want to create a new dataframe row that combines the two rows (by name because I have to do this for multiple sets of rows of the df) into one row like this:

   Jan  Feb
a   1    3
b   2    4  
c  1/2  3/4 

And I do not mean just division as the data types of the actual values are string, I want to concatenate. I feel like there has to be an obvious solution but nothing I have found online is working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

df.loc['c', :] = df.apply(lambda x: f"{x[0]}/{x[1]}")
print(df)

Output:

   Jan  Feb
a  1.0  3.0
b  2.0  4.0
c  1/2  3/4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading