Pandas Introduction
- One-way pandas allow you to work with data is a dataframe.
- Let’s go through the process to go from a comma-separated values
(.csv)
file to a dataframe. - This variable
csv_path
stores the path of the.csv,
that is used as an argument to theread_csv
function. - The result is stored in the object df; this is a common short form used for a variable referring to a Pandas dataframe.
read CSV python pandas
import pandas as pd df = pd.read_csv('books.csv') # read .csv file.
# Print first five rows of the dataframe df.head()
S.No | Name | Author |
---|---|---|
1 | The Color Purple | Alice Walker |
2 | The 5 Second Rule | Mel Robbins |
3 | Tuesdays with Morrie | Mitch Albom |
4 | Own It | Sallie Krawcheck |
Read data from CSV File and print the first five rows
# Read data from CSV file path = 'abcd.csv' df = pd.read_csv(path)
- We can use the method
head()
to examine the first five rows of a dataframe.
# Print first five rows of the dataframe df.head()
Read data from Excel File and print the first five rows.
# Read data from CSV file path = 'abcd.xlsx' df = pd.read_excel(path) df.head()
- We use the path of the excel file and the function read_excel.
# Print first five rows of the dataframe df.head()
- The result is a data frame as above