Reading an excel file using pandas

The name pandas actually comes from “panel data”, an economics term.

Reading an actual file excel and working on it, using pandas is much faster and easier than opening an excel file and working on it.

You have to import the following module so as to read the files:

import pandas as pd

Importing it as “pd” reduces the effort while calling it many times.

To read a .xlsx file:

df = pd.read_excel(“path to .xlsx file”)

To read a .csv file:

df = pd.read_csv(“path to .csv file”)

Here I have loaded  the files into a DataFrame “df”, DataFrame is a 2D data structure.

Below is an example of how to work with the file:

pandas_rel

If you want the names of the employees who work in the company “Wok N Roll”:

df[df[‘Company’] == ‘Wok N Roll’][‘Name’]

If you want the names of the employees who work in the company “Kung Food” and have sales greater than $500:

df[df[‘Company’] == ‘Kung Food’ & df[‘Sales’] > 500][‘Name’]

If you want to convert a DataFrame column to list:

df[‘Name’].tolist()