Export Pandas DataFrame to CSV without Index and Header

In this tutorial, we will learn how to export pandas DataFrame to CSV without index and header? By Pranit Sharma Last updated : April 18, 2023

Pandas DataFrame to CSV without Index

To export Pandas DataFrame to CSV without an index, you can specify the index=False parameter inside the DataFrame.to_csv() method which writes/exports DataFrame to CSV by ignoring the index.

Syntax

df.to_csv("path", sep="," , index=False)

Pandas DataFrame to CSV without Header

To export Pandas DataFrame to CSV without a header, you can specify the header=False parameter inside the DataFrame.to_csv() method which writes/exports DataFrame to CSV by ignoring the header.

Syntax

df.to_csv("path", sep="," , header=False)

Pandas DataFrame to CSV without Index and Header

To export Pandas DataFrame to CSV without index and header, you can specify both parameters index=False and header=False inside the DataFrame.to_csv() method which writes/exports DataFrame to CSV by ignoring the index and header.

Syntax

df.to_csv("path", sep="," , index=False, header=False)

Let us understand with the help of an example.

Python Code to Export Pandas DataFrame to CSV without Index and Header

# Importing Pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {
    "Jason":[69,74,77,72],
    "Jim":[65,96,65,86],
    "Will":[87,97,85,51],
    "Nick":[66,68,85,94]
}

# Now, Create DataFrame
df=pd.DataFrame(d)

# Exporting DF to CSV without Index and Header
df.to_csv("mycsv1.csv",sep=",",header=False,index=False)

Output (CSV File Content is):

69,65,87,66
74,96,97,68
77,65,85,85
72,86,51,94

Python Pandas Programs »

All Python Pandas Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.