ADVERTISEMENT
ADVERTISEMENT

Writing a pandas DataFrame to CSV file

Learn how to write a pandas DataFrame to CSV file?
Submitted by Pranit Sharma, on April 17, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data. DataFrame can be created with the help of python dictionaries or lists but in the real world, CSV files are imported and then converted into DataFrames. Sometimes, DataFrames are first written into CSV files. Here, we are going to write a DataFrame into a CSV file.

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.

Here, the separator character (,) is called 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.

pandas.DataFrame.to_csv() Method

Write object to a comma-separated values (csv) file.

Syntax:

DataFrame.to_csv(
    path_or_buf=None, 
    sep=',', 
    na_rep='', 
    float_format=None, 
    columns=None, 
    header=True, 
    index=True, 
    index_label=None, 
    mode='w', 
    encoding=None, 
    compression='infer', 
    quoting=None, 
    quotechar='"', 
    line_terminator=None, 
    chunksize=None, 
    date_format=None, 
    doublequote=True, 
    escapechar=None, 
    decimal='.', 
    errors='strict', 
    storage_options=None
    )

    # In short
    DataFrame.to_csv('path_of_folder\name_of_file')

To work with 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.

Example:

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {
    "Name":['Hari','Mohan','Neeti','Shaily','Ram','Umesh'],
    "Age":[25,36,26,21,30,33],
    "Gender":['Male','Male','Female','Female','Male','Male'],
    "Profession":['Doctor','Teacher','Singer','Student','Engineer','CA'],
    "Title":['Mr','Mr','Ms','Ms','Mr','Mr']
}

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

# Printing the original DataFrame
print("Original DataFrame:\n")
print(df,"\n\n")

# Now, Create a csv file and load it 
# into a specified folder
df.to_csv("F:\All study\my_csv.csv")

print("CSV file created\n")
print("Importing this csv file:\n\n")

# We can check the csv file by navigating into the specified 
# folder or by importing it through pd.read_csv()
data = pd.read_csv("F:\All study\my_csv.csv")

# Printing the Data
print(data)

Output:

Write a pandas DataFrame to CSV file | Output

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.