Monday, October 30, 2017

Reading csv and excel files in pandas dataframe

Dataframe can be used to load the data from various files. Let's look into how we can load the data from excel and csv.

Make sure the data file that you are loading is present in the given location. In my case, I will be assuming that the data file is present at the same location where python code is.

Code to load different files

import pandas as pd

#install xlrd module to support excel.
#In python 3 use: pip3 install xlrd
#read the excel file by giving name and sheet
excel_df = pd.read_excel('titanic.xls',sheetname=0)

#Writing the csv file out
#By default index also goes in csv. It can be ignored
#sep is optional. By default it's comma

#Reading the csv file is similar
#Let's read the same csv file that we generated
csv_df = pd.read_csv('titanic.csv')

No comments:

Post a Comment