JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Convert Pandas to CSV without index

Author:JIYIK Last Updated:2025/05/01 Views:

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates, and other unique columns as indexes for DataFrames.

But sometimes when exporting or reading files, users may not need this extra index column. The following DataFrame will illustrate this problem more clearly.

import pandas as pd

df = pd.DataFrame([[6, 7, 8], [9, 12, 14], [8, 10, 6]], columns=["a", "b", "c"])

print(df)

Output:

   a   b   c
0  6   7   8
1  9  12  14
2  8  10   6

As you can see, we added an extra index to the DataFrame, which the user can avoid when saving it to a file. If we want to convert this DataFrame into a CSV file without an index column, we can do so by setting to to_csv()in the function .indexFalse

Example code.

import pandas as pd

df = pd.DataFrame([[6, 7, 8], [9, 12, 14], [8, 10, 6]], columns=["a", "b", "c"])

print(df)

df.to_csv("data2.csv", index=False)

Output:

   a   b   c
0  6   7   8
1  9  12  14
2  8  10   6

As you can see from the output, the DataFrame does have an index, but since we indexset the parameter to False, the exported CSV file does not have the extra column.

If we export a file with an extra index column (without indexsetting the parameter to False), and then try to read it, we will get a strange extra column.

import pandas as pd

df = pd.DataFrame([[6, 7, 8], [9, 12, 14], [8, 10, 6]], columns=["a", "b", "c"])

print(df)

df.to_csv("data2.csv")

df_new = pd.read_csv("data2.csv")

print(df_new)

Output:

   a   b   c
0  6   7   8
1  9  12  14
2  8  10   6
   Unnamed: 0  a   b   c
0           0  6   7   8
1           1  9  12  14
2           2  8  10   6

As you can see, df_newthe DataFrame has an extra Unnamedcolumn.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.to_dict() Function

Publish Date:2025/05/01 Views:188 Category:Python

Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary. pandas.DataFrame.to_dict() Syntax DataFrame . to_dict(orient = 'dict' , into = class ' dict ' ) parameter orient This parameter determines the type of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial