如何读取范围('A5:B10')并使用 openpyxl 将这些值放入数据框中
How can I read a range('A5:B10') and place these values into a dataframe using openpyxl
能够以类似于 excel 的方式定义范围,即 'A5:B10' 对我需要的内容很重要,因此将整个 sheet 读取到数据帧并不是很重要有用。
所以我需要做的是将 Excel sheet 中多个范围的值读取到多个不同的数据帧。
valuerange1 = ['a5:b10']
valuerange2 = ['z10:z20']
df = pd.DataFrame(values from valuerange)
df = pd.DataFrame(values from valuerange1)
或
df = pd.DataFrame(values from ['A5:B10'])
我已经搜索过了,但要么是我的搜索工作做得很差,要么其他人都解决了这个问题,但我真的做不到。
谢谢。
使用 openpyxl
既然你已经指出,你正在寻找一种非常用户友好的方式来指定范围(如 excel-syntax)并且正如 Charlie Clark 已经建议的那样,你可以使用 openpyxl。
以下实用函数采用一个工作簿和一个 column/row 范围和 returns 一个 pandas DataFrame:
from openpyxl import load_workbook
from openpyxl.utils import get_column_interval
import re
def load_workbook_range(range_string, ws):
col_start, col_end = re.findall("[A-Z]+", range_string)
data_rows = []
for row in ws[range_string]:
data_rows.append([cell.value for cell in row])
return pd.DataFrame(data_rows, columns=get_column_interval(col_start, col_end))
用法:
wb = load_workbook(filename='excel-sheet.xlsx',
read_only=True)
ws = wb.active
load_workbook_range('B1:C2', ws)
输出:
B C
0 5 6
1 8 9
Pandas只有解决方案
给定 excel sheet 中的以下数据:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
您可以使用以下命令加载它:
pd.read_excel('excel-sheet.xlsx')
如果您要限制读取的数据,pandas.read_excel 方法提供了多种选择。使用 parse_cols
、skiprows
和 skip_footer
来 select 您要加载的特定子集:
pd.read_excel(
'excel-sheet.xlsx', # name of excel sheet
names=['B','C'], # new column header
skiprows=range(0,1), # list of rows you want to omit at the beginning
skip_footer=1, # number of rows you want to skip at the end
parse_cols='B:C' # columns to parse (note the excel-like syntax)
)
输出:
B C
0 5 6
1 8 9
一些注意事项:
read_excel 方法的 API 并不意味着支持更复杂的 select 离子。如果您需要一个复杂的过滤器,将整个数据加载到 DataFrame 并使用 excellent 切片和索引机制 provided by pandas.
会更容易(也更清晰)
最简单的方法是使用 pandas 从 excel.
获取值的范围
import pandas as pd
#if you want to choose single range, you can use the below method
src=pd.read_excel(r'August.xlsx',usecols='A:C',sheet_name='S')
#if you have multirange, which means a dataframe with A:S and as well some other range
src=pd.read_excel(r'August.xlsx',usecols='A:C,G:I',sheet_name='S')
能够以类似于 excel 的方式定义范围,即 'A5:B10' 对我需要的内容很重要,因此将整个 sheet 读取到数据帧并不是很重要有用。
所以我需要做的是将 Excel sheet 中多个范围的值读取到多个不同的数据帧。
valuerange1 = ['a5:b10']
valuerange2 = ['z10:z20']
df = pd.DataFrame(values from valuerange)
df = pd.DataFrame(values from valuerange1)
或
df = pd.DataFrame(values from ['A5:B10'])
我已经搜索过了,但要么是我的搜索工作做得很差,要么其他人都解决了这个问题,但我真的做不到。
谢谢。
使用 openpyxl
既然你已经指出,你正在寻找一种非常用户友好的方式来指定范围(如 excel-syntax)并且正如 Charlie Clark 已经建议的那样,你可以使用 openpyxl。
以下实用函数采用一个工作簿和一个 column/row 范围和 returns 一个 pandas DataFrame:
from openpyxl import load_workbook
from openpyxl.utils import get_column_interval
import re
def load_workbook_range(range_string, ws):
col_start, col_end = re.findall("[A-Z]+", range_string)
data_rows = []
for row in ws[range_string]:
data_rows.append([cell.value for cell in row])
return pd.DataFrame(data_rows, columns=get_column_interval(col_start, col_end))
用法:
wb = load_workbook(filename='excel-sheet.xlsx',
read_only=True)
ws = wb.active
load_workbook_range('B1:C2', ws)
输出:
B C
0 5 6
1 8 9
Pandas只有解决方案
给定 excel sheet 中的以下数据:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
您可以使用以下命令加载它:
pd.read_excel('excel-sheet.xlsx')
如果您要限制读取的数据,pandas.read_excel 方法提供了多种选择。使用 parse_cols
、skiprows
和 skip_footer
来 select 您要加载的特定子集:
pd.read_excel(
'excel-sheet.xlsx', # name of excel sheet
names=['B','C'], # new column header
skiprows=range(0,1), # list of rows you want to omit at the beginning
skip_footer=1, # number of rows you want to skip at the end
parse_cols='B:C' # columns to parse (note the excel-like syntax)
)
输出:
B C
0 5 6
1 8 9
一些注意事项:
read_excel 方法的 API 并不意味着支持更复杂的 select 离子。如果您需要一个复杂的过滤器,将整个数据加载到 DataFrame 并使用 excellent 切片和索引机制 provided by pandas.
会更容易(也更清晰)最简单的方法是使用 pandas 从 excel.
获取值的范围import pandas as pd
#if you want to choose single range, you can use the below method
src=pd.read_excel(r'August.xlsx',usecols='A:C',sheet_name='S')
#if you have multirange, which means a dataframe with A:S and as well some other range
src=pd.read_excel(r'August.xlsx',usecols='A:C,G:I',sheet_name='S')