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: 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.
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>
data
The variable now contains all the data in the specified column.
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>
data
The 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!