追加空数据框和列表索引超出范围错误

时间:2017-05-30 03:06:50

标签: python loops for-loop

The Line 2:13 is the existing data format which I want it in the format as dispalyed in line 16:19

我写的代码是

import xlrd
import pandas as pd

book = xlrd.open_workbook(
    "C:/Users/Vinod/Desktop/DataSet Mining/python-pandas.xlsx")
sheet = book.sheet_by_name('Sheet1')

df1 = pd.DataFrame()

n = sheet.nrows
print(n)
n2 = sheet.ncols
print(n2)

for row_index in range(0, n):
    for col_index in range(0, n2):

        col = sheet.cell(row_index, col_index).value
        df = pd.DataFrame()
        l1 = []
        l2 = []
        l3 = []
        l4 = []
        l5 = []
        l6 = []
        if (col == "Entity Name:"):
            EntityName = sheet.cell(row_index, col_index + 1).value

        if (col == "Counterparty Name:"):
            CounterpartyName = sheet.cell(row_index, col_index + 1).value
        if (col == "TradeRef"):
            row_index = row_index + 1
            value_1 = sheet.cell(row_index, col_index).value

            while (value_1 != ""):

                TradeRef = sheet.cell(row_index, col_index).value

                TradeDate = sheet.cell(row_index, col_index + 1).value

                TradeType = sheet.cell(row_index, col_index + 2).value
                ConfirmationMedium = sheet.cell(row_index, col_index + 3).value

                l1.append(TradeRef)
                l2.append(TradeDate)
                l3.append(TradeType)
                l4.append(ConfirmationMedium)

                row_index = row_index + 1
                if (row_index >= n):
                    value_1 = ""
                    col_index = 4


                else:
                    value_1 = sheet.cell(row_index, col_index).value
                    print(value_1)

            df['TradeRef'] = l1
            df['TradeDate'] = l2
            df['TradeType'] = l3
            df['ConfirmationMedium'] = l4
            df['EntityName'] = EntityName
            df['CoutrepartyName'] = CounterpartyName
            print(df)
            print(row_index)
            print(col_index)
            df1.append(df)
print(df1)

1 个答案:

答案 0 :(得分:0)

I got it resolved @Azat 
import xlrd
import pandas as pd
book = xlrd.open_workbook("C:/Users/Vinod/Desktop/DataSet Mining/python-pandas.xlsx")
sheet = book.sheet_by_name('Sheet1')
df1=pd.DataFrame()
n=sheet.nrows
n2=sheet.ncols
for row_index in range(0,n):
    for col_index in range(0,n2):

        if(row_index)<n:
            col=sheet.cell(row_index,col_index).value
            df=pd.DataFrame()
            l1=[]
            l2=[]
            l3=[]
            l4=[]
            l5=[]
            l6=[]
            if(col=="Entity Name:"):

                EntityName=sheet.cell(row_index,col_index+1).value


            if(col=="Counterparty Name:"):
                CounterpartyName=sheet.cell(row_index,col_index+1).value
            if(col=="TradeRef"):
                row_index=row_index+1
                value_1=sheet.cell(row_index,col_index).value

                while( value_1!=""):





                  TradeRef=sheet.cell(row_index,col_index).value

                  TradeDate=sheet.cell(row_index,col_index+1).value


                  TradeType=sheet.cell(row_index,col_index+2).value
                  ConfirmationMedium=sheet.cell(row_index,col_index+3).value

                  l1.append(TradeRef)
                  l2.append(TradeDate)
                  l3.append(TradeType)
                  l4.append(ConfirmationMedium)

                  row_index=row_index+1
                  if(row_index>=n):

                      value_1=""
                      col_index=4

                  else:
                     value_1=sheet.cell(row_index,col_index).value

                df['TradeRef']=l1
                df['TradeDate']=l2
                df['TradeType']=l3
                df['ConfirmationMedium']=l4
                df['EntityName']=EntityName
                df['CoutrepartyName']=CounterpartyName
                df1=df1.append(df)
print(df1)