Search code examples
pythonpython-3.xpandasdataframeexport-to-csv

How to export data to csv sequentially from multiple data frames in a sequential order


I have multiple dataframes like below.

DF1:

enter image description here

DF2

enter image description here

DF3

enter image description here

I want to store them to a file like below.So the data should be in sequential order from each dataframe. Can any one help me to find a way to store in the below format in a file.

enter image description here


Solution

  • Use df.append:

    df = DF1.append(DF2).append(DF3)
    

    Then write it to a file using df.to_csv() file:

    df.to_csv('test.csv')
    

    OR to an excel file using df.to_excel():

    df.to_excel('test.xls')
    

    OR (as per @anky 's comment): use pd.concat

    df_list = [DF1,DF2,DF3] ## create a list with your dataframes
    df = pd.concat(df_list)
    
    df.to_csv('test.csv')