Q:

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

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to convert pandas DataFrame to NumPy array?... >>
<< Import multiple csv files into pandas and concaten...