In Python, How Do you Save a Dataframe as a csv File?

Pandas Module in Python:

Pandas is an open-source library based on the NumPy library. It enables users to perform effective data analysis, cleaning, and preparation. Pandas is fast, and it provides users with high performance and productivity.

The majority of the datasets you work with are referred to as DataFrames. DataFrames is a 2-Dimensional labeled Data Structure containing indexes for rows and columns, and each cell can store any form of value. DataFrames are simply Dictionary-based NumPy Arrays.

Creating a Dataframe

Approach:

  • Import os module using the import keyword.
  • Import pandas module using the import keyword.
  • Give some random list(fruits) as static input and store it in a variable.
  • Create a dictionary for the list of the above fruits and store it in another variable.
  • Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function to create a dataframe for the above dictionary.
  • Print the above-obtained dataframe.
  • The Exit of the Program.

Below is the implementation:

# Import os module using the import keyword.
import os
# Import pandas module using the import keyword.
import pandas
# Give some random list(fruits) as static input and store it in a variable.
fruit_lst = ["Apple", "mango", "banana", "orange", "grapes", "Kiwi"]
# Create a dictionary for the above fruits list and store it in another variable.
fruit_dictnry = {'Types of Fruits': fruit_lst}
# Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function
# to create a dataframe for the above dictionary.
rslt_dataframe = pandas.DataFrame(fruit_dictnry)
# Print the above obtained dataframe.
print(rslt_dataframe)

Output:

 Types of Fruits
0           Apple
1           mango
2          banana
3          orange
4          grapes
5            Kiwi

How to Save this Dataframe?

We frequently encounter circumstances in which we need to save large amounts of data generated by scraping or analysis in an easy, accesible  and readable form rather shareable form.

We can achieve this now by storing the data frame as a csv file.

dataframe.to_csv('filename.csv')

We can save a data frame as a CSV file using the pandas.to_csv() function. The file name must be passed as a parameter to the method.

Example

Approach:

  • Import os module using the import keyword.
  • Import pandas module using the import keyword.
  • Give some random list(fruits) as static input and store it in a variable.
  • Create a dictionary for the list of the above fruits and store it in another variable.
  • Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function to create a dataframe for the above dictionary.
  • Save the above dataframe as a CSV file using the to_csv() function by passing some random file name as an argument that you want to save with.
  • Now the file will be saved as a csv file.
  • The Exit of the Program.

Below is the implementation:

from pandas.core.frame import DataFrame
# Import os module using the import keyword.
import os
# Import pandas module using the import keyword.
import pandas
# Give some random list(fruits) as static input and store it in a variable.
fruit_lst = ["Apple", "mango", "banana", "orange", "grapes", "Kiwi"]
# Create a dictionary for the above fruits list and store it in another variable.
fruit_dictnry = {'Types of Fruits': fruit_lst}
# Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function
# to create a dataframe for the above dictionary.
rslt_dataframe = pandas.DataFrame(fruit_dictnry)
# Save the above dataframe as a CSV file using the to_csv() function by passing 
# some random file name as an argument that you want to save with.
rslt_dataframe.to_csv('fruits.csv')

Output:

Types of Fruits
0 Apple
1 mango
2 banana
3 orange
4 grapes
5 Kiwi