Search code examples
pandasdataframetranspose

Create a transposed one-row dataframe


I have a dataframe of the form

                   name     product_name    current_page
DATI_BENE          NaN      NaN             4.0
DATI_PERSONALI     NaN      NaN             4.0
HOMEPAGE           NaN      NaN             4.0
Next_Command       4.0      NaN             NaN
Start_Procedure    8.0      NaN             NaN
Valore_Auto        NaN      12.0            NaN

Out of it, I would like to make a new one with only one row ignoring the NaNs, like

DATI_BENE   DATI_PERSONALI  HOMEPAGE    Next_Command    Start_Procedure     Valore_Auto
4.0         4.0             4.0         4.0             8.0                 12.0

Is there an uncomplicate way to do it?


Solution

  • You can try;

    df.stack().to_frame().droplevel(1).T
    #or df.ffill(1).iloc[:,-1].rename(None).to_frame().T
    

       DATI_BENE  DATI_PERSONALI  HOMEPAGE  Next_Command  Start_Procedure  \
    0        4.0             4.0       4.0           4.0              8.0   
    
       Valore_Auto  
    0         12.0