Built with Tutify.io

    AIStudyLabs

    Export Pandas to CSV without Index & Header

    Export Pandas to CSV without Index & Header

    In order to export Pandas DataFrame to CSV without an index (no row indices) use param index=False and to ignore/remove header use header=False param on to_csv() method. In this article, I will explain how to remove the index and header on the CSV file with examples. Note that this method also supports several other parameters to write Pandas DataFrame to CSV file.

    Key Points –

    <a id="h-pandas-to-csv-without-index-amp-header"></a>

    Pandas to CSV without Index & Header

    By default, when exporting a Pandas DataFrame to CSV, it includes the column names in the first row and the row index in the first column. It also writes a file using a comma-separated delimiter to separate columns. However, the to_csv() method in Pandas DataFrame offers parameters that allow you to omit the index and header during writing.

    To run some examples of exporting pandas to CSV without index & header, let's create a Pandas DataFrame.

    
    # Create a DataFrame
    import pandas as pd
    import numpy as np
    technologies = {
        'Courses':["Spark","PySpark","Hadoop","Python"],
        'Fee' :[22000,25000,np.nan,24000],
        'Duration':['30day',None,'55days',np.nan],
        'Discount':[1000,2300,1000,np.nan]
              }
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-pandas-to-csv-with-no-index"></a>

    Pandas to CSV with no Index

    You can export a Pandas DataFrame to a CSV file without including the index by using the index=False parameter in the to_csv() method. This allows you to exclude the index when writing or exporting the DataFrame to a CSV file.

    
    # Remove column header and index
    df.to_csv("c:/tmp/courses.csv",index=False)
    

    Writes courses.csv file without index.

    
    # Output:
    Courses,Fee,Duration,Discount
    Spark,22000.0,30day,1000.0
    PySpark,25000.0,,2300.0
    Hadoop,,55days,1000.0
    Python,24000.0,,
    

    <a id="h-pandas-to-csv-without-header"></a>

    Pandas to CSV without Header

    To write DataFrame to CSV without a column header (remove column names) use header=False param on to_csv() method.

    
    # Remove header while writing
    df.to_csv("c:/tmp/courses.csv",header=False)
    

    Writes courses.csv file as.

    
    # Output:
    0,Spark,22000.0,30day,1000.0
    1,PySpark,25000.0,,2300.0
    2,Hadoop,,55days,1000.0
    3,Python,24000.0,,
    

    <a id="h-export-without-index-and-header"></a>

    Export without Index and Header

    Let's combine these and see how you can use both removing index and column names on the CSV files.

    
    # Remove header & index while writing
    df.to_csv("c:/tmp/courses.csv",header=False, index=False)
    

    <a id="h-faq-on-export-pandas-to-csv-without-index-amp-header"></a>

    FAQ on Export Pandas to CSV without Index & Header

    You can export a DataFrame to a CSV file without including the index by setting the index=False parameter in the to_csv() method.

    To export a DataFrame without both the index and header, use the index=False and header=False parameters in the to_csv() method.

    The index parameter determines whether the DataFrame index is written to the CSV file. By setting index=False, the index will be excluded.

    The header parameter specifies whether to write the column names to the CSV file. Setting header=False prevents column names from being written.

    You can exclude the index and header when exporting a subset of the DataFrame by selecting the relevant part first and then calling to_csv() on that part with index=False and header=False.

    <a id="h-conclusion"></a>

    Conclusion

    In this quick article, I have explained remove the header without column names and index while writing DataFrame to a CSV file. Use param header=False to remove columns and use index=False to remove index (no row indices) on to_csv() method.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles