读取特定单元格范围的特定列并使用 Pandas 存储值

Read a specific column of a certain cell range and store the values using Pandas

我正在尝试找出一种方法来从特定单元格范围的特定列读取数据并使用 pandas 将其存储到数组中。

例如我的 Excel sheet 包括:


测试 | p

食物|价格

鸡肉| 8.54

牛肉 |6.73

蔬菜| 3.2

总价 |18.47

注意:第一行有一个空 space 是有原因的。 注意: |表示细胞分离。

我正在尝试获取从 B3 行到 B5 行的价格值,并通过 [8.54,6.73,3.2] 将它们存储到数组中。

到目前为止我的代码是:

import pandas as pd

xl_workbook = pd.ExcelFile("readme.xlsx")  # Load the excel workbook
df = xl_workbook.parse("Sheet1")  # Parse the sheet into a dataframe
x1_list = df['p'].tolist()  # Cast the desired column into a python list
print(x1_list)

然后结果为 [nan, u'price',8.54,6.73,3.2]

如果我只想读取值 8.54、6.73 和 3.2,得到 [8.54,6.73,3.2],我该怎么做?

有没有办法抓取某个单元格范围的某个列?

正如所写,您可以在 Pandas 中使用 read_excel。这假设您具有一致的格式。

import pandas as pd

# define the file name and "sheet name"
fn = 'Book1.xlsx'
sn = 'Sheet1'

data = pd.read_excel(fn, sheetname=sn, index_col=0, skiprows=1, header=0, skip_footer=1)