Search code examples
pythonpandasconcatenationrowoperation

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.


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