Home > Backend Development > Python Tutorial > How to read a column of excel data in python

How to read a column of excel data in python

下次还敢
Release: 2024-03-29 06:45:31
Original
1189 people have browsed it

How to read a column of Excel data in Python: Pandas library: import the library and read the file. Select the column and store it in the data variable. Openpyxl library: loading files and selecting worksheets. Select the column and loop through the cells, storing the values ​​in the data list.

How to read a column of excel data in python

How to read a column of Excel data in Python

Method 1: Use the Pandas library

Pandas is for data analysis and processing powerful library. To read a column of Excel data using Pandas, you can follow these steps:

<code class="python">import pandas as pd

# 读取Excel文件
df = pd.read_excel('file.xlsx')

# 选择要读取的一列
column_name = 'Column_Name'
data = df[column_name]</code>
Copy after login

dataThe variable now contains all the data in the specified column.

Method 2: Use the Openpyxl library

Openpyxl is a library for reading and writing Excel files. To read a column of Excel data using Openpyxl, follow these steps:

<code class="python">import openpyxl

# 加载Excel文件
wb = openpyxl.load_workbook('file.xlsx')

# 选择要读取的工作表
sheet = wb.active

# 选择要读取的一列
column_index = 1  # 根据需要更改列索引

# 遍历列中的所有单元格
data = []
for row in sheet.rows:
    cell = row[column_index-1]
    data.append(cell.value)</code>
Copy after login

dataThe list now contains all the data in the specified column.

The above is the detailed content of How to read a column of excel data in python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template