Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Openpyxl and Pandas | Openpyxl
Excel Automation with Python (project)

book
Openpyxl and Pandas

Let's now import an excel file and transform it into a pandas DataFrame!

Using openpyxl and pandas together allowing for efficient data analysis and the creation of feature-rich Excel reports directly from Python.

Uppgift

Swipe to start coding

  • Import pandas and openpyxl;
  • Extract the values of the workbook:
    • Use load_workbook() function that reads the file from the disk so that you can manipulate it;
    • workbook.active help get access to the currently active worksheet in the workbook;
  • sheet.values property generates a generator of all the rows in the worksheet, where each row is represented as a tuple of cell values.
  • Create a DataFrame (df) out of them:
    • Use pd.DataFrame() function.

Lösning

import pandas as pd
from openpyxl import load_workbook

workbook = load_workbook(filename="sample_data.xlsx")
sheet = workbook.active

values = sheet.values
df = pd.DataFrame(values)
df

Mark tasks as Completed
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 10
some-alt