在Python字典中存储电子表格的列
我在Excel文件中存储了一个表,如下所示: Species Garden Hedgerow Parkland Pasture Woodland Blackbird 47 10 40 2 2 Chaffinch 19 3 5 0 2 Great Tit 50 0 10 7 0 House Sparrow 46 16 8 4 0 Robin 9 3 0 0 2 Song Thrush 4 0 6 0 0 很明显,在Python字典中存储电子表格的列,python,excel,dictionary,xlrd,Python,Excel,Dictionary,Xlrd,我在Excel文件中存储了一个表,如下所示: Species Garden Hedgerow Parkland Pasture Woodland Blackbird 47 10 40 2 2 Chaffinch 19 3 5 0 2 Great Tit 50 0 10 7 0 House Sparr
headers
是一个存储列标题的简单列表,sdata
包含表数据,存储为列表列表。以下是它们的外观:
标题:
[u'Species', u'Garden', u'Hedgerow', u'Parkland', u'Pasture', u'Woodland']
sdata:
[[u'Blackbird', 47.0, 10.0, 40.0, 2.0, 2.0], [u'Chaffinch', 19.0, 3.0, 5.0, 0.0, 2.0], [u'Great Tit', 50.0, 0.0, 10.0, 7.0, 0.0], [u'House Sparrow', 46.0, 16.0, 8.0, 4.0, 0.0], [u'Robin', 9.0, 3.0, 0.0, 0.0, 2.0], [u'Song Thrush', 4.0, 0.0, 6.0, 0.0, 0.0]]
但我想将这些数据存储到Python字典中,每列作为包含每列所有值的列表的键。例如(仅显示部分数据以节省空间):
所以,我的问题是:我如何才能做到这一点?我知道我可以像上面的代码片段那样按列而不是按行读取数据,但我不知道如何将列存储在字典中
提前感谢您提供的任何帮助。一旦您有了专栏,就相当容易了:
dict(zip(headers, sdata))
实际上,在您的示例中,sdata
可能是行数据,即使如此,这仍然相当简单,您也可以使用zip
转换表:
dict(zip(headers, zip(*sdata)))
这两个选项中的一个是您所要求的。1。XLRD
我强烈推荐使用库中的defaultdict。每个键的值将以默认值启动,在本例中为空列表。
我并没有在那个里放那个么多异常捕获,您可能希望根据您的用例添加异常检测
import xlrd
import sys
from collections import defaultdict
result = defaultdict(list)
workbook = xlrd.open_workbook("/Users/datafireball/Desktop/stackoverflow.xlsx")
worksheet = workbook.sheet_by_name(workbook.sheet_names()[0])
headers = worksheet.row(0)
for index in range(worksheet.nrows)[1:]:
try:
for header, col in zip(headers, worksheet.row(index)):
result[header.value].append(col.value)
except:
print sys.exc_info()
print result
输出:
defaultdict(<type 'list'>,
{u'Garden': [47.0, 19.0, 50.0, 46.0, 9.0, 4.0],
u'Parkland': [40.0, 5.0, 10.0, 8.0, 0.0, 6.0],
u'Woodland': [2.0, 2.0, 0.0, 0.0, 2.0, 0.0],
u'Hedgerow': [10.0, 3.0, 0.0, 16.0, 3.0, 0.0],
u'Pasture': [2.0, 0.0, 7.0, 4.0, 0.0, 0.0],
u'Species': [u'Blackbird', u'Chaffinch', u'Great Tit', u'House Sparrow', u'Robin', u'Song Thrush']})
u'Species', u'Garden', u'Hedgerow', u'Parkland', u'Pasture', u'Woodland']
{u'Garden': [47.0, 19.0, 50.0, 46.0, 9.0, 4.0], u'Hedgerow': [10.0, 3.0, 0.0, 16.0, 3.0, 0.0], u'Pasture': [2.0, 0.0, 7.0, 4.0, 0.0, 0.0], u'Parkland': [40.0, 5.0, 10.0, 8.0, 0.0, 6.0], u'Woodland': [2.0, 2.0, 0.0, 0.0, 2.0, 0.0], u'Species': [u'Blackbird', u'Chaffinch', u'Great Tit', u'House Sparrow', u'Robin', u'Song Thrush']}
您无法想象使用dataframe可以获得多大的灵活性
Species Garden Hedgerow Parkland Pasture Woodland
0 Blackbird 47 10 40 2 2
1 Chaffinch 19 3 5 0 2
2 Great Tit 50 0 10 7 0
3 House Sparrow 46 16 8 4 0
4 Robin 9 3 0 0 2
5 Song Thrush 4 0 6 0 0
如果XLRD不能解决你的问题,考虑一下。其中一个示例视频演示了如何从Excel表格中获取数据并将其导入Pandas dataframe,该dataframe比字典更有用
如果你真的想要一本字典,熊猫可以很容易地转换成字典,请参阅。我将为自己的问题提供另一个答案 在我发布我的问题之后,我发现了一个非常小的Python库,它充当其他电子表格处理包(即xlrd和odfpy)的包装器。它有一个很好的todict方法,它完全符合我的要求(即使不需要转置表) 下面是一个使用上述数据的示例:
from pyexcel import SeriesReader
from pyexcel.utils import to_dict
sheet = SeriesReader("Sample.xls")
print sheet.series() #--- just the headers, stored in a list
data = to_dict(sheet)
print data #--- the full dataset, stored in a dictionary
输出:
defaultdict(<type 'list'>,
{u'Garden': [47.0, 19.0, 50.0, 46.0, 9.0, 4.0],
u'Parkland': [40.0, 5.0, 10.0, 8.0, 0.0, 6.0],
u'Woodland': [2.0, 2.0, 0.0, 0.0, 2.0, 0.0],
u'Hedgerow': [10.0, 3.0, 0.0, 16.0, 3.0, 0.0],
u'Pasture': [2.0, 0.0, 7.0, 4.0, 0.0, 0.0],
u'Species': [u'Blackbird', u'Chaffinch', u'Great Tit', u'House Sparrow', u'Robin', u'Song Thrush']})
u'Species', u'Garden', u'Hedgerow', u'Parkland', u'Pasture', u'Woodland']
{u'Garden': [47.0, 19.0, 50.0, 46.0, 9.0, 4.0], u'Hedgerow': [10.0, 3.0, 0.0, 16.0, 3.0, 0.0], u'Pasture': [2.0, 0.0, 7.0, 4.0, 0.0, 0.0], u'Parkland': [40.0, 5.0, 10.0, 8.0, 0.0, 6.0], u'Woodland': [2.0, 2.0, 0.0, 0.0, 2.0, 0.0], u'Species': [u'Blackbird', u'Chaffinch', u'Great Tit', u'House Sparrow', u'Robin', u'Song Thrush']}
希望它也有帮助 此脚本允许您将excel数据转换为词汇表列表
import xlrd
workbook = xlrd.open_workbook('Sample.xls')
workbook = xlrd.open_workbook('Sample.xls', on_demand = True)
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock names of columns
for col in range(worksheet.ncols):
first_row.append( worksheet.cell_value(0,col) )
# tronsform the workbook to a list of dictionnary
data =[]
for row in range(1, worksheet.nrows):
elm = {}
for col in range(worksheet.ncols):
elm[first_row[col]]=worksheet.cell_value(row,col)
data.append(elm)
print data
顺便说一句,pandas一次就完成了所有这些,生成了一个dataframe对象,它可以像你的字典一样使用。这将为人们提供一种简单的方法来测试他们的答案:例如,将你拥有的转化为你想要的。谢谢你的建议,Emilio,我会提供。谢谢,我确实知道这可以在熊猫身上实现,但出于几个原因,我正在寻找一个更直接的解决方案(正如你和其他人所提供的!)。
import xlrd
workbook = xlrd.open_workbook('Sample.xls')
workbook = xlrd.open_workbook('Sample.xls', on_demand = True)
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock names of columns
for col in range(worksheet.ncols):
first_row.append( worksheet.cell_value(0,col) )
# tronsform the workbook to a list of dictionnary
data =[]
for row in range(1, worksheet.nrows):
elm = {}
for col in range(worksheet.ncols):
elm[first_row[col]]=worksheet.cell_value(row,col)
data.append(elm)
print data