CodeGym /Courses /Python SELF EN /Reading Data from Excel Files and Converting Them into Da...

Reading Data from Excel Files and Converting Them into DataFrame

Python SELF EN
Level 27, Lesson 2
Available

1. Core Methods for Working with Excel Files in pandas

Reading Data with read_excel

To start working with Excel files, first install pandas and openpyxl, if you haven’t done so already. These two libraries are like a trusty pair of old boots—without them, you’re not stepping into the world of data analysis.

Bash

pip install pandas openpyxl

Now we’re ready to dive into the spreadsheets. pandas offers a simple and convenient method read_excel, which lets you load data from Excel files into a DataFrame—the standard data format in pandas.

Python

import pandas as pd

# Reading data from an Excel file
data = pd.read_excel('example.xlsx')

# Displaying the first five rows of the DataFrame
print(data.head())

That’s it! We’ve read data from an Excel file and converted it into a DataFrame. In just a few microseconds, we have data on-screen that would have required opening Excel before.

Data Reading Parameters

The read_excel method supports numerous parameters that let you read data from a specific sheet, a particular range of cells, or specify data formats.

  • sheet_name: Specifies the sheet name or its index (starting from 0) that you want to read. For example, sheet_name='Sheet1' or sheet_name=0.
  • usecols: Allows you to select specific columns to import. For instance, usecols="A:C" to select only the first three columns.
  • skiprows: Lets you skip the first N rows in the file. This is useful for dropping headers or unnecessary data at the start of the file.
Python

# Reading data from a specific sheet and selecting columns
data_filtered = pd.read_excel('example.xlsx', sheet_name='Sheet1', usecols="A:C", skiprows=2)

2. Transforming Data into a DataFrame

What is a DataFrame?

A DataFrame is like the electronics in a robotic vacuum cleaner: it may look simple on the outside (yeah, just a table), but it’s what helps the vacuum navigate, and for the DataFrame, to know what data to store and how to process it.

In pandas, the DataFrame is a two-dimensional data structure with row (index) and column labels. Unlike standard Python lists or NumPy arrays, DataFrame lets you work with data as if it were a table in a database or Excel.

Basic Operations with DataFrame

One of the coolest features of DataFrame in pandas is how easy it is to interact with the data. For instance, you can sort data, filter it, or choose subsets of rows and columns.

Selecting Columns

Selecting a column is as simple as using its name. Just like clicking the column header in Excel to select it. Couldn't be easier:

Python

# Selecting a single column
dates = data['Date']

# Selecting multiple columns
subset = data[['Name', 'Salary']]

Selecting Rows

If you need to access rows, you can use the iloc method for indexing and loc for working with labels.

Python

# Selecting the first row
first_row = data.iloc[0]

# Selecting rows with a condition
high_salary = data[data['Salary'] > 50000]

3. Examples and Practice

Now that we have a basic understanding of data collections in DataFrame, let’s practice a bit. Imagine we have an Excel file data.xlsx with multiple sheets, and we want to extract data from a specific sheet, process it, and print it to the console.

Practical Exercise

Your task: Write a script that reads data from the data.xlsx file, selects the Sales sheet, and filters sales above 1000 units.

Python

# Reading data from the 'Sales' sheet and filtering
sales_data = pd.read_excel('data.xlsx', sheet_name='Sales')
high_sales = sales_data[sales_data['Amount'] > 1000]

print(high_sales)

This exercise will let you feel the pandas magic and become a data wizard. Sure, no actual magic will happen, but you’ll turn raw tables into useful info—and in the analytics world, that’s real magic!

4. Errors and Implementation Details

Beginners often forget little details, like column names being case-sensitive, or that pandas assumes the first row as headers by default. If your data is different, you might run into errors. Be friends with your data: always check column names after loading a file using print(data.columns).

Another important note: if you're working with files from big corporations, the data might be encrypted. pandas won’t help there, but a strong cup of coffee and a break might!

All of these insights will help you automate the mundane parts of dealing with Excel data. Automating these processes not only saves you time but also gets rid of the logistical nightmares tied to copying and pasting. Your Python scripts will act as automatic assistants, instantly preparing report data.

1
Task
Python SELF EN, level 27, lesson 2
Locked
Reading Data from Excel
Reading Data from Excel
2
Task
Python SELF EN, level 27, lesson 2
Locked
Reading data from a specific sheet
Reading data from a specific sheet
3
Task
Python SELF EN, level 27, lesson 2
Locked
Data Filtering
Data Filtering
4
Task
Python SELF EN, level 27, lesson 2
Locked
Reading and Transforming Data
Reading and Transforming Data
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION