1. pandas operation Excel spreadsheet
There is an Excel file contains a two-page sheet
Read Excel files in two ways:
# Method, the default read first form Import PANDAS AS pd DF = pd.read_excel ( " filename " ) # default open first Excel spreadsheet Data = df.head () # Default Read first five Print ( " Get all the values: \ n-{0} " .format ( Data)) # format output
# Method Two: to read the name of the form specified by way Import PANDAS time pd DF = pd.read_excel ( ' lemon.xlsx ' , sheet_name = ' Student ' ) # can be specified by the read sheet_name Form Data = df.head () # data before the default read line 5 Print ( " Get all values: \ n-{0} " .format (Data)) # format output
# Method three: a form specified by the index to access a form, a form represents 0 # dual form name and the mode index can be used to locate the form # may also be positioned at the same time form a plurality, are listed as follows embodiment df = pd.read_excel ( ' lemon.xlsx ' , SHEET_NAME = [ ' Python ' , ' Student ' ]) # can be specified by name at the same time form a plurality of # DF = pd.read_excel ( 'lemon.xlsx', SHEET_NAME = 0) may # form specified by the read index sheet # DF = pd.read_excel ( 'lemon.xlsx', SHEET_NAME = [ 'Python',. 1]) may be mixed # way to specify # DF = pd.read_excel ( 'lemon.xlsx ', sheet_name = [1,2]) # index can be specified by a plurality of simultaneous data = df.values # acquired all the data, can not pay attention to this head () method oh ~ Print ( " Get all the values are:\n{0}" .Format (Data)) # format output
2. pandas operations the ranks of Excel
1 Read specified single row, there will be data list Import PANDAS AS PD DF = pd.read_excel ( ' lemon.xlsx ' ) # This default read directly into the first form Excel Data df.ix = [0] .values # 0 is the first row and read the data does not contain header, to pay attention to Oh! Print ( " read data specified line: \ n-{0} " .format (Data)) 2 Read multiple lines specified df=pd.read_excel('lemon.xlsx') Data . df.ix = [[1,2]] values # reads the specified multiple rows, it is necessary IX [] a list of specified number of lines nested inside Print ( " read data specified row: \ n {0} " .format (the Data)) 3 Read the ranks specified df=pd.read_excel('lemon.xlsx') data = df.ix [[1,2], [ ' title ' , ' data ' ]]. values # reads the value in the title column of the first row and the second data line, where the need nested list Print ( " Read data taking the specified row: \ n-{0} " .format (data)) 4 Read multiple columns and rows specified value df=pd.read_excel('lemon.xlsx') data = df.ix [[1,2], [ ' title ' , ' data ' ]]. values # reads the value in the title column of the first row and the second data line, where the need nested list Print ( " Read data taking the specified row: \ n-{0} " .format (data)) 5 Get all the rows of the specified column df=pd.read_excel('lemon.xlsx') data = df.ix [:, [ ' title ' , ' data ' ]] values. # read title data and the value of the column for all rows, where the need nested list Print ( " read data specified line: \ n { } 0 " .format (Data)) 6 . Gets the line number and print out DF = pd.read_excel ( ' lemon.xlsx ' ) Print ( " List Output line numbers " , df.index.values) 7 . Gets the column name and print output DF = pd.read_excel ( ' lemon.xlsx ' ) Print ( " output column heading " , df.columns.values) 8 . Gets the value of a specified number of rows: DF = pd.read_excel ( ' lemon.xlsx ' ) Print ( " output value " , df.sample (. 3) .values) # This method is similar head () method, and a method df.values 9 . Gets the value of the specified column DF = pd.read_excel ( ' lemon.xlsx ' ) Print ( " output value \ n- " , DF [ ' Data ' ] .values)
3. pandas become Excel Data Dictionary
workbook = pd.read_excel(self.excelpath, sheet_name=self.sheet) test_data = [] for i in workbook.index.values: if self.sheet == 0: row_data = workbook.ix[i, ["case_id", "title", "data"]].to_dict() elif self.sheet == 1: row_data = workbook.ix[i, ["name", "age", "ks", "ksh", "money", "phone"]].to_dict() else: row_data = workbook.ix[ i, ["case_id", "case_name", "case_data", "case_header", "case_method", "case_url", "case_port", "case_yq_reault" ]] test_data.append(row_data)