We store data in file in our computer system. When we want to use it we load the data in our program. There are different types of files and every file has different way to get loaded in program.
Here we’ll see some of them.
- Excel file-
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.
pip install xlrd
Now we can read the data in excel file by importing xlrd in python.
- Excel file by using pandas-
We can import pandas and read an xlsx file.
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.
- CSV file- A CSV file stands for comma separated values file is a text based format represents data found in spreadsheet.
we have to directly import csv module from python packages. And then we are able to read the csv file by following the given syntax:-
import csv filename="file_name.csv" csv_reader=csv.reader(filename) print(csv_reader)
- CSV file using pandas-
we have to import pandas module from the python packages.
import pandas as pd data=pd.read_csv("path of file") print(data)
We can also read and print some specific columns and rows.
In this way we can load dataset in python.