Hello Internet, in this tutorial we are going to learn how to read and write csv files with the help of Pandas DataFrame. In the world of data analysis, working with structured datasets is essential, and CSV (Comma-Separated Values) files are one of the most common formats used for storing and exchanging tabular data. Whether you’re handling financial records, sales reports, or large-scale datasets, being able to efficiently read and write CSV files is a fundamental skill for any data analyst or Python programmer. Python’s Pandas library provides powerful tools to effortlessly load, manipulate, and save CSV files using DataFrames—a flexible, high-performance data structure designed for handling structured data. In this tutorial, we will explore how to read CSV files into Pandas DataFrames, manipulate the data, and write DataFrames back to CSV files. By these, you’ll be able to streamline your data workflows and enhance your data processing capabilities.
What is a DataFrame?
In simple words (Layman’s term), DataFrame helps to create a Table (rows and columns). A DataFrame is a two-dimensional, tabular data structure in Pandas, similar to a spreadsheet or SQL table. It consists of rows and columns, where each column can have a different data type (e.g., integers, floats, strings). DataFrames make it easy to store, manipulate, and analyze structured data in Python.
How to Read a CSV File with Pandas DataFrame
There are 2 ways to create a Pandas DataFrame.
- Reading a .csv file to create a DataFrame
import pandas as pd df = pd.read_csv("Data.csv") print(df)
Output:
Name Age City Data Salary 0 Ayush 25 Delhi 2023-01-01 50000 1 Rohit 30 Mumbai 2023-01-05 60000 2 Sanjay 35 Pune 2023-01-09 70000 3 Kabir 40 Bangaluru 2023-01-12 80000
- Creating a DataFrame Manually
import pandas as pd data = { 'Name': ['Ayush', 'Rohit', 'Sanjay', 'Kabir'], 'Age': [25, 30, 35, 40], 'City': ['Delhi', 'Mumbai', 'Pune', 'Bangaluru'], 'Date': pd.to_datetime(['2023-01-01', '2023-01-05', '2023-01-09', '2023-01-12']), 'Salary': [50000, 60000, 70000, 80000] } df = pd.DataFrame(data) print(df)
Output:
Name Age City Data Salary 0 Ayush 25 Delhi 2023-01-01 50000 1 Rohit 30 Mumbai 2023-01-05 60000 2 Sanjay 35 Pune 2023-01-09 70000 3 Kabir 40 Bangaluru 2023-01-12 80000
We can also read speacific columns as well
# Read only Name and Age columns df_subset = pd.read_csv('Data.csv', usecols=['Name', 'Age']) print("\nSelected Columns:") print(df_subset)
Output:
Selected Columns: Name Age 0 Ayush 25 1 Rohit 30 2 Sanjay 35 3 Kabir 40
How to Write a DataFrame
- Appending a new Data
new_data = { 'Name': ['Aisha'], 'Age': [28], 'City': ['Gurugram'], 'Date': ['18-01-2023'], 'Salary': [65000] } df_new = pd.DataFrame(new_data) df_new['Date'] = pd.to_datetime(df_new['Date']) df_new.to_csv('Data.csv', mode='a', header=False, index=False) updated_df = pd.read_csv('Data.csv', parse_dates=['Date']) print("\nNew Data Appended:") print(updated_df)
Output:
New Data Appended: Name Age City Date Salary 0 Ayush 25 Delhi 01-01-2023 50000 1 Rohit 30 Mumbai 05-01-2023 60000 2 Sanjay 35 Pune 09-01-2023 70000 3 Kabir 40 Bangaluru 12-01-2023 80000 4 Aisha 28 Gurugram 2023-01-18 65000
Conclusion
In this tutorial, we explored how to read and write CSV files using the Pandas DataFrame—an essential skill for anyone working with data in Python. We learned how to load data from a CSV file, create DataFrames manually, select specific columns, and append new data to an existing file. With just a few lines of code, Pandas simplifies complex data operations and makes your data manipulation tasks more efficient and readable. Whether you’re dealing with small tables or large datasets, mastering these basic CSV operations lays a solid foundation for more advanced data analysis and automation workflows. Keep experimenting, and let Pandas do the heavy lifting in your data projects!
Happy Coding!