ADVERTISEMENT
ADVERTISEMENT

How to avoid Pandas creating an index in a saved CSV?

Learn how to avoid Pandas creating an index in a saved CSV?
Submitted by Pranit Sharma, on April 20, 2022

CSV Files:

CSV files or Comma Separated Values files are plain text files but the format of CSV files is tabular. As the name suggests, in a CSV file, each specific value inside the CSV file is generally separated with a comma. The first line identifies the name of a data column. The further subsequent lines identify the values in rows.

Col_1_value, col_2_value ,  col_3_value
Row1_value1, row_1_value2, row_1_value3

Here, the separator character (,) is called as the delimiter. There are some more popular delimiters. E.g.: tab(\t), colon (:), semi-colon (;) etc.

We will use DataFrame.to_csv() method to write DataFrame into CSV file. It takes an argument in the form of a DataFrame name which has to be written in a CSV file. It also requires the path of the specified folder where the user wants to create the CSV file.

Syntax:

DataFrame.to_csv('path_of_folder/name_of_file')

When we write a DataFrame into a CSV file, an unwanted column of indices is also created into the CSV file. As an analyst, we don't want this unwanted column, to avoid this, we have to set index=False and pass it as a parameter while creating a CSV file.

To work with MultiIndex in Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand with the help of an example.

# 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)
df.to_csv("E:/mycsv1.csv",sep=",",header=False,index=False)

Output:

Avoid Pandas creating an index in a saved CSV

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.