![]() |
VOOZH | about |
A CSV (Comma Separated Values) file is a plain text file where each line represents a data record, and fields within each record are separated by commas. It's commonly used for spreadsheets and databases due to its simplicity and readability.
Below are some operations that we perform while working with Python CSV files in Python
Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Pythonβs built-in open() function, which returns a file object. In this example, we first open the CSV file in READ mode, file object is converted to csv.reader object and further operation takes place. Code and detailed explanation is given below.
Output
π ImageThe above example uses a CSV file aapl.csv which can be downloaded from here .
Explanation:
We can read a CSV file into a dictionary using the csv module in Python and the csv.DictReader class. Here's an example:
Suppose, we have a employees.csv file and content inside it will be:
name,department,birthday_month
John Smith,HR,July
Alice Johnson,IT,October
Bob Williams,Finance,January
Example: This reads each row as a dictionary (headers as keys), then appends it to list .
Output:
{'name': 'John Smith', 'department': 'HR', 'birthday_month': 'July'}
{'name': 'Alice Johnson', 'department': 'IT', 'birthday_month': 'October'}
{'name': 'Bob Williams', 'department': 'Finance', 'birthday_month': 'January'}
Explanation:
To write to a CSV file, we first open the CSV file in WRITE mode. The file object is converted to csv.writer object and further operations takes place. Code and detailed explanation is given below.
Explanation:
To write a dictionary to a CSV file, the file object (csvfile) is converted to a DictWriter object. Detailed example with explanation and code is given below.
Output
Consider that a CSV file looks like this in plain text:
Explanation:
We can read a Python CSV files with Pandas using pandas.read_csv() function. Here's an example:
Suppose, we have a employees.csv file and content inside it will be:
name,department,birthday_month
John Smith,HR,July
Alice Johnson,IT,October
Bob Williams,Finance,January
In this example, pd.read_csv() reads the CSV file into a Pandas DataFrame. The resulting DataFrame can be used for various data manipulation and analysis tasks.
Output:
name department birthday_month
0 John Smith HR July
1 Alice Johnson IT October
2 Bob Williams Finance January
We can access specific columns, filter data, and perform various operations using pandas DataFrame functionality. For example, if we want to access the "name" column, we can use df['name'].
Output :
0 John Smith
1 Alice Johnson
2 Bob Williams
Name: name, dtype: object
We can use Pandas to write CSV files. It can done by using pd.DataFrame() function. In this example, the Pandas library is used to convert a list of dictionaries (mydict) into a DataFrame, representing tabular data. The DataFrame is then written to a Python CSV file named "output.csv" using the to_csv method, creating a structured and readable data file for further analysis or sharing.
Output CSV File:
branch,cgpa,name,year
COE,9.0,Nikhil,2
COE,9.1,Sanchit,2
IT,9.3,Aditya,2
SE,9.5,Sagar,1
MCE,7.8,Prateek,3
EP,9.1,Sahil,2
We start by importing the csv module and use it to store names and emails as comma-separated values. Using the open() function, we create a CSV file, and then write each row using a writer object, with separate columns for names and emails.
Output: