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

python combine 2 columns into 1 row

Hello there i have the following dataframe:


   Name      Lastname     ticket        
0   Peter       Pan        Ticket1
1   Null       Null         $20      

i want to make it look like this:

   Name      Lastname     ticket   ticketprice     
0   Peter       Pan        Ticket1      $20 
1    NULL       NULL        Null        Null

but it seems to be really difficult for me. Does anyone know how this works?

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 :

Given that your odd columns contain the ticket price, one way you could do this is by copying the column into another ticketprice column and shifting that column up by one like so:

import pandas as pd
    
df['ticket_price'] = df['ticket'].shift(-1)
df = df.iloc[::2]

Then delete all rows that have an even index to remove the empty line. In the example above df is the pandas dataframe containing your base data.

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