Search code examples
pythonpandastext-processing

How can i create a text file from a pandas dataframe mixing two columns in an alternate form?


Lets say i have two columns on a same huge dataframe (more than 1G of data) in pandas something like this (simplified):

df

A B
C D

and i want to create a text file with all data in the form below:

A
B
C
D

how would you do this?


Solution

  • There are several ways. You could use .stack() or .melt() or .unstack() or pd.concat():

    input:

    0 1
    A B
    C D
    

    #1:

    pd.DataFrame(df.stack().reset_index(drop=True))
    

    #2:

    pd.DataFrame(df.melt().iloc[:,-1])
    

    #3:

    pd.DataFrame(df.unstack().reset_index(drop=True)) #keeps order as A, C, B, D
    

    #4:

    pd.DataFrame(pd.concat([df['0'],df['1']]).reset_index(drop=True)) #keeps order as A, C, B, D
    

    output:

    0
    A
    B
    C
    D