Reading CSV Files With Pandas
We can read a Python CSV files with Pandas using the 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.
Python3
import pandas as pd # Read the CSV file into a DataFrame df = pd.read_csv( 'employees.csv' ) # Display the DataFrame print (df) |
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'].
Python3
# Access the 'name' column names = df[ 'name' ] print (names) |
Output:
0 John Smith
1 Alice Johnson
2 Bob Williams
Name: name, dtype: object
Working with csv files in Python
Python is one of the important fields for data scientists and many programmers to handle a variety of data. CSV (Comma-Separated Values) is one of the prevalent and accessible file formats for storing and exchanging tabular data.
In article explains What is CSV. Working with CSV files in Python, Reading, and Writing to a CSV file, and Storing Emails in CSV files.
Table of Content
- What is a CSV File?
- Working with CSV files in Python
- Reading a CSV file
- Reading CSV Files Into a Dictionary With csv
- Writing to a CSV file
- Writing a dictionary to a CSV file
- Reading CSV Files With Pandas
- Writing CSV Files with Pandas
- Storing Emails in CSV files
Contact Us