How to import excel file in python?
Excel file is a spreadsheet which makes us easy to store data in a tabular form. Now the question is that how can we read data from the excel file while writing a python program?
Firstly, we have to install xlrd module.
syntax:-
pip install xlrd
Now we can read the data in excel file by importing xlrd in python.
syntax:-
import xlrd location_of_file=("path of file") a=xlrd.open(location_of_file) print(a)
In this way we can read excel file by importing xlrd.
We can also print data of a particular row or column.
syntax:-
import xlrd location_of_file=("path of file") a=xlrd.open(location_of_file) sheet_name.column_value(0,0) print(sheet_name.column_value(0,0))
We have one more method to read excel file without xlrd.
We can import pandas and read an excel file.
syntax:-
import pandas as pd data=pd.read_excel("file.xlsx", file_name='Sheet1') print(data)
We can also read a data of a particular column or row by using pandas module.
syntax:-
import pandas as pd data=pd.read_excel('location_of_file') d=pd.dataframe(data, column['column_name']) print(d)
We can also retrieve data of rows by calling their name as we did in columns.
This is all about importing data from an Excel file in python.