Reputation: 71
I'm working on a program that needs to take rows I have in a table from pandas and store each one as a dictionary entry with one column as the key and the others stored in an object with that key. It seems to work ok and I get no errors when it's looping to add them to the dictionary, but if I try to call a specific item from that dictionary using syntax like SeqDict['key'].classattribute, I'm getting an error saying "'set' object has no attribute 'classattribute'. Am I using the wrong syntax or is there something else wrong with my code? This is the first time I have ever tried programming so sorry if I have made a stupid mistake or my code is written very inefficiently.
The first half of my code seems to work fine
import os
import pandas as pd
print("Please move your CSV to the desktop before continuing.")
csvfilename = input("Enter the name of your CSV file: ")
desktopfilepath = os.path.expanduser("~/Desktop/")
csvfilepath = desktopfilepath + csvfilename
seqtable = (pd.read_csv(csvfilepath,
dtype=str,
header=0,
usecols=["Well", "Core Sample ID", "Sample ID", "Exon"]))
newseqtable = pd.DataFrame.dropna(seqtable)
print(newseqtable)
y = len(newseqtable.index)
print(y, "files to rename.")
I'm assuming this part is where I'm doing something wrong
class SeqID:
def __init__(self,well,sequence,exon):
self.well = well
self.sequence = sequence
self.exon = exon
SeqDict = {}
x = 0
for x in range(y):
ID = newseqtable.iat[x,1]
well = newseqtable.iat[x,0]
sequence = newseqtable.iat[x,2]
exon = newseqtable.iat[x,3]
# print(ID + "," + well + "," + sequence + "," + exon + ".") #This works fine when I choose to run it
SeqDict[ID] = {SeqID(well,sequence,exon)}
SeqDict #This prints the dictionary keys I would expect
SeqDict['key'].well # This returns the error when I insert a key
Thanks
Upvotes: 0
Views: 276
Reputation: 799520
This line:
SeqDict[ID] = {SeqID(well,sequence,exon)}
creates a 1-set. You probably meant:
SeqDict[ID] = SeqID(well,sequence,exon)
Upvotes: 2