Managing CSV Files with
What is a CSV File?
• A CSV file is the most common, simple, and easiest method to
store tabular data.
• This particular format arranges tables by following a specific
structure divided into rows and columns. It is these rows and
columns that contain your data.
• The core purpose of the CSV format is to help you present the
tabular data compactly and concisely.
• A new line terminates each row to start the next row. Similarly, a
comma, also known as the delimiter, separates columns within
each row.
Example-
City, State, Capital, Population
New York, New York, No, 8.623 Million
Austin, Texas, Yes, 0.95 Million
Miami, Florida, No, 0.463 Million
Reading CSV Files
Once you go through the installation, you can use
the read_csv() function to read a CSV file. We will try to read the
“nba.csv" file, which we have uploaded earlier.
• First we have to import the Pandas library:
import pandas as pd
# reading the csv
data = pd.read_csv("nba.csv")
• import pandas as pd
When we execute this code, it will read the CSV file “nba.csv" from the
current directory. You can see from the script above that to read a CSV
file, you have to pass the file path to the read_csv() method of the Pandas
library. The read_csv() method then returns a Pandas DataFrame that
contains the data of the CSV file.
You can display the first five rows of the CSV file via the head() method of
the Pandas DataFrame, as shown below:
data.head()
Pandas Functions- Data I/O
Pandas Functions- Data Preview
Pandas Functions- Data Cleansing
Functions- Data Transformation
Functions- Data Aggregation
Pandas Functions- Slicing
https://www.sharpsightlabs.com/blog/pandas-iloc/
Practice session
• We are going explore many datasets in this last session
QUIZ
• What are csv files?
• Write basic code for reading csv files in pandas?
• Write the program for reading titanic.csv file and show
only 10 rows?
• How to add only specific column while reading a csv file ?
• Write code for Read csv file to Dataframe with custom
delimiter(,) ?
Thank you