Pandas: Read specific Excel cell value into a variable
Reading an Excel file using Pandas is going to default to a dataframe. You don't need an entire table, just one cell. The way I do it is to make that cell a header, for example:
# Read Excel and select a single cell (and make it a header for a column)
data = pd.read_excel(filename, 'Sheet2', index_col=None, usecols = "C", header = 10, nrows=0)
Will return a "list" of 1 header(s) and no data. Then isolate that header:
# Extract a value from a list (list of headers)
data = data.columns.values[0]
print (data)
Elaborating on @FLab's comment use something along those lines:
Edit:
Updated the answer to correspond to the updated question that asks how to read some sheets at once.
So by providing sheet_name=None
to read_excel()
you can read all the sheets at once and pandas return a dict
of DataFrames, where the keys are the Excel sheet names.
import pandas as pd
In [10]:
df = pd.read_excel('Book1.xlsx', sheetname=None, header=None)
df
Out[11]:
{u'Sheet1': 0
0 1
1 1, u'Sheet2': 0
0 1
1 2
2 10}
In [13]:
data = df["Sheet1"]
secondary_data = df["Sheet2"]
secondary_data.loc[2,0]
Out[13]:
10
Alternatively, as noted in this post, if your Excel file has several sheets you can pass sheetname
a list of strings, sheet names to parse eg.
df = pd.read_excel('Book1.xlsx', sheetname=["Sheet1", "Sheet2"], header=None)
Credits to user6241235 for digging out the last alternative