Issue
I have parsed data in dataframes by using Pandas. I need to insert the data into class that I created.
My class:
class Message:
time = None
id = None
type = None
source = None
destination = None
def __init__(self, time, id, type, source, destination):
self.time = time
self.id = id
self.type = type
self.source = source
self.destination = destination
I'm going through the dataframes and trying to insert the output into the attributes of the class as following:
newMessage=Message()
for index, row in df.iterrows():
newMessage.__init__(row['time'], row['id'], row['type'], row['source'], row['destination'])
print(row['time'], row['ID'], row['TYPE'], row['Source'], row['Destination'])
The exception it throws:
TypeError: __init__() missing 7 required positional arguments
I don't know how to call the class and the arguments, please help.
Solution
You could try something like that:
class Message:
time = None
id_ = None
type_ = None
source = None
destination = None
def __init__(self, time, id_, type_, source, destination):
self.time = time
self.id_ = id_
self.type_ = type_
self.source = source
self.destination = destination
for index, row in df.iterrows():
newTelegram = Message(row['time'], row['id'], row['type'], row['source'], row['destination'])
print(newTelegram.time)
print(getattr(newTelegram, "time"))
Note that both id & type are keywords in Python.
Answered By - Marcel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.