tegge-classroom / STAT2984-2018

STAT 2984: Statistical Programming I - Spring 2018
1 stars 13 forks source link

Merging Data Sets #44

Open abbyt99 opened 6 years ago

abbyt99 commented 6 years ago

We have read in both data sets, but when we tried to merge then it said that the list index is out of range. This is the code that we used to try to merge the sets. `f2_data={}

for line in f2: l=line.split(',') if l[0] in f2_data: print l print f2_data[l[0]] f2_data[l[0]]=l

print f2_data

data=[] for row in f_data: if row[18] not in f2_data: print row else: data.append(row+f2_data[row[0]])

print data`

cahicks commented 6 years ago

The common variable is "State". In the f data, "State" is in row 18 and if the f2 data set, "State" is in row 0

ategge commented 6 years ago

Please include the error message when posting bugs.

The error message will likely tell you which line of code produced the error. If you print out your variables in the line prior, you may gain some insights as to what is going on.

abbyt99 commented 6 years ago

IndexError Traceback (most recent call last)

in () 13 data=[] 14 for row in f_data: ---> 15 if row[18] not in f2_data: 16 print row 17 else: IndexError: list index out of range This is the error message
ategge commented 6 years ago

The error message is saying that the list represented by row does not have at least 19 elements. This could be a special instance of row that does not have enough elements or all instances of row. I recommend putting a print row just before the if statement in line 15 to see what the specific row that is throwing the error looks like.

cahicks commented 6 years ago

read in file 1

f=open(filename, 'r').read().splitlines() f_data=[] for line in f: f_data.append(line.split(','))

read in file2

f2=open(filename2, 'r').read().splitlines() f2_data={} for line in f2: l=line.split(',')

merge data

print row

data=[] for row in f_data: if row[0] not in f2_data: print row else: data.append(row + f2_data[row[0]])

Here is the code with us printing the row. When we printed the row, the last row of our f_data came up. I don't know what that means. I do not know how to fix the row error