• Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Print the">

    Writing a Pandas DataFrame to CSV file



    To write a Pandas DataFrame to CSV file, we can take the following Steps −

    Steps

    • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

    • Print the input DataFrame.

    • Use df.to_csv to save the values of the DataFrame to a CSV (comma-separated values) file.

    Example

     Live Demo

    import pandas as pd
    
    df = pd.DataFrame(
       {
          "x": [5, 2, 1, 9],
          "y": [4, 1, 5, 10],
          "z": [4, 1, 5, 0]
       }
    )
    
    print "Input DataFrame is:
    ", df df.to_csv("test.csv", sep='\t')

    Output

    Input DataFrame is:
       x   y  z
    0  5   4  4
    1  2   1  1
    2  1   5  5
    3  9  10  0

    It will create a new file ("test.csv") and save the values of the DataFrame in it.

    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements