Commit b9816665 authored by panos's avatar panos Committed by Jérome Perrin

Insert two scripts, ImportExceldata to import data from Excel files and...

Insert two scripts, ImportExceldata to import data from Excel files and ReplaceMissingValues to replace the missing values in a data sample
parent d7df8b39
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DREAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
'''
Created on 19 Feb 2014
@author: Panos
'''
#The Import_Excel object
class Import_Excel:
#This object reads data from different worksheets of an Excel document
def Input_data(self,worksheetName,inputbook): #This method takes as an argument the Name of the preferred worksheet
workbook = inputbook
worksheet = workbook.sheet_by_name(worksheetName)
num_rows = worksheet.nrows
num_cols = worksheet.ncols
title=[] #Create a new list
i=1
for i in range(num_cols):
title.append(worksheet.cell_value(0, i)) #Put in this new list the names of the first row (attributes)of the worksheet
print title #Print the new list
i=2
j=1
worksheetDict = {} #Create a python dictionary
for i in range(num_cols):
tempList = [] #Create a new list
for j in range(1,num_rows):
tempList.append(worksheet.cell_value(j,i)) #Put in this list the cells' values of each column in the worksheet
if title[i] != '':
worksheetDict.update({title[i]: tempList}) #Update the dictionary only with excel columns with existed data, give as a key the name of the name of the attribute (e.g. Moulding)
return worksheetDict #Return the created dictionary
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DREAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
'''
Created on 19 Feb 2014
@author: Panos
'''
#The HandleMissingValues object
class HandleMissingValues:
#Three different approaches to handle missing values are included in this object
def ReplaceWithZero(self,mylist): #Replace in the given list the missing values with zero
i=0
for value in mylist:
if value is '' :
mylist[i]=0.0 #For the values in the list if there is a missing value replace it with zero
i+=1
return mylist #Return the replaced list
def DeleteMissingValue(self,mylist): #Delete the missing value in the given list
i=0
listX=[] #Create a new list
for value in mylist:
if value != '':
listX.append(value) #Add in the new list the non missing values from the given list
i+=1
print 'after del', mylist
return listX #Return the new list, which has the non missing values from the initial given list
def ReplaceWithMean(self,mylist): #Replace in the given list the missing values with the mean value
list1=self.DeleteMissingValue(mylist) #Create a new list, which is the given list deleting the missing values (calling the DeleteMissingValue method)
mean=sum(list1)/float(len(list1)) #Calculate the mean value of the new list
print mean
i=0
for value in mylist:
if value is '' :
mylist[i]=mean #For the values in the initial given list if there is a missing value replace it with the mean value of the new list
i+=1
return mylist #Return the given list, in which the missing values are replaced with the mean value
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment