2014-10-09 14 views
6

Ho una tabella memorizzata in un file Excel come segue:Memorizzazione delle colonne di un foglio di calcolo in un dizionario Python

 
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 

Sto usando la libreria xlrd Python per la lettura di questi dati. Non ho alcun problema la lettura in una lista di liste (con ogni riga della tabella memorizzata come una lista), usando il codice seguente:

from xlrd import open_workbook 
wb = open_workbook("Sample.xls") 
headers = [] 
sdata = [] 
for s in wb.sheets(): 
    print "Sheet:",s.name 
    if s.name.capitalize() == "Data": 
     for row in range(s.nrows): 
      values = [] 
      for col in range(s.ncols): 
       data = s.cell(row,col).value 
       if row == 0: 
        headers.append(data) 
       else: 
        values.append(data) 
      sdata.append(values) 

Come è probabilmente ovvio, headers è un semplice elenco memorizzare le intestazioni delle colonne e sdata contiene i dati della tabella, memorizzati come un elenco di elenchi. Ecco quello che sembrano:

intestazioni:

[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]] 

Ma voglio memorizzare questi dati in un dizionario Python, con ogni colonna come chiave per un elenco contenente tutti i valori per ogni colonna. Per esempio (solo una parte dei dati vengono visualizzati per risparmiare spazio):

dict = { 
    'Species': ['Blackbird','Chaffinch','Great Tit'], 
    'Garden': [47,19,50], 
    'Hedgerow': [10,3,0], 
    'Parkland': [40,5,10], 
    'Pasture': [2,0,7], 
    'Woodland': [2,2,0] 
} 

Quindi, la mia domanda è: come posso raggiungere questo obiettivo? So che potrei leggere i dati per colonne anziché per righe come nel frammento di codice sopra, ma non sono riuscito a capire come memorizzare le colonne in un dizionario.

Grazie in anticipo per qualsiasi tipo di assistenza è possibile fornire.

+3

A proposito, panda fa tutto questo in una sola volta, producendo un oggetto dataframe, che può essere utilizzato tanto come il dizionario. – mdurant

+0

Ti suggerisco di pubblicare l'elenco delle liste che hai ora. Darebbe alle persone un modo semplice per testare le loro risposte: trasformare ciò che hai in ciò che vuoi, per questo esempio. –

+0

Grazie per il suggerimento, Emilio, lo fornirò. – maurobio

risposta

2

Una volta che avete le colonne, è abbastanza facile:

dict(zip(headers, sdata)) 

In realtà, sembra che sdata nel tuo esempio possono essere i dati di riga, anche così, che è ancora abbastanza facile, è possibile trasporre la tabella con zip così:

dict(zip(headers, zip(*sdata))) 

Uno di questi due è quello che state chiedendo.

3

1. XLRD

Si consiglia vivamente di utilizzare defaultdict dalla libreria collections. Il valore di ciascuna chiave verrà avviato con il valore predefinito, in questo caso una lista vuota. Non ho inserito così tante eccezioni in questo caso, si potrebbe voler aggiungere il rilevamento delle eccezioni in base al proprio caso d'uso.

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 

uscita:

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']}) 

2. Pandas

import pandas as pd 
xl = pd.ExcelFile("/Users/datafireball/Desktop/stackoverflow.xlsx") 
df = xl.parse(xl.sheet_names[0]) 
print df 

Output, e non puoi immaginare quanta flessibilità puoi ottenere utilizzando 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 
+0

Grazie, in effetti sapevo che ciò sarebbe stato possibile ottenere in panda, ma per diverse ragioni cercavo una soluzione più diretta (come te e gli altri forniti!). – maurobio

1

Se XLRD non risolve il problema, si consideri guardando XLWings.Uno dei video di esempio mostra come prelevare dati da una tabella Excel e importarli in un dataframe di Pandas, che sarebbe più utilizzabile di un dizionario.

Se si in realtà desidera un dizionario, i Panda possono convertirlo facilmente, vedere here.

2

Contribuirò a me stesso, fornendo ancora un'altra risposta per la mia stessa domanda!

Subito dopo aver postato la mia domanda, ho scoperto pyexcel - una piccola libreria Python che funge da wrapper per altri pacchetti di gestione di fogli di calcolo (ovvero xlrd e odfpy). Ha un bel metodo to_dict che fa esattamente quello che voglio (anche senza bisogno di trasporre la tabella)!

Ecco un exemple, utilizzando i dati di cui sopra:

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 

uscita:

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']} 

Speranza che aiuta anche!

1

Questo script consentono di trasformare un dato excel alla lista dei Dictionnary

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 
Problemi correlati