All Courses

How to read excel cell values as a list?

By Manerushi149@gmail.com, a month ago
  • Bookmark
0

How to read excel cell values as a list?

Pandas
Python
1 Answer
0
Goutamp777

There are several ways to read excel cell values as a list:


  1. Using openpyxl library:
import openpyxl


# load the workbook
wb = openpyxl.load_workbook('example.xlsx')


# select the sheet
sheet = wb.active


# read the cell values and store them in a list
cell_values = []
for row in sheet.iter_rows():
    for cell in row:
        cell_values.append(cell.value)


print(cell_values)


2. Using pandas library:

import pandas as pd


# read the excel file
df = pd.read_excel('example.xlsx')


# convert the dataframe to a list
cell_values = df.values.tolist()


print(cell_values)


3. Using xlrd library:

import xlrd


# open the workbook
wb = xlrd.open_workbook('example.xlsx')


# select the sheet
sheet = wb.sheet_by_index(0)


# read the cell values and store them in a list
cell_values = []
for row in range(sheet.nrows):
    for col in range(sheet.ncols):
        cell_values.append(sheet.cell(row, col).value)


print(cell_values)





Your Answer

Webinars

How To Land a Job in Data Science?

Apr 6th (7:00 PM) 190 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More