How can I convert each Pandas Data Frame row into an object including the column values as the attributes?
Question:
Suppose I have a DataFrame including following columns "NAME", "SURNAME", "AGE" and I would like to create one object for each row, including those column values as its variables.
person = ConvertRow2Object(frame.iloc[0,:])
print person.NAME //outputs Gary
How can I do it with a generic solution to any DataFrame with any kind of column names and data types?
Answers:
You can convert the whole thing to a numpy recarray, then each record in the array is attributed:
people = frame.to_records()
person = people[0]
print person.NAME # etc...
Using a namedtuple also seems to work:
from collections import namedtuple
Person = namedtuple('Person', frame.dtypes.index.tolist())
person = Person(*frame.iloc[0,:])
print person.NAME # etc...
This technique of creating dictionary objects and passing that as init argument worked for me
#AppInfo = Class which I wanted to create instance objects for each row in df
def AppInfo:
def __init__(self, attr_dict):
if attr_dict is not None:
for key, value in attr_dict.items():
setattr(self, key, value)
# in my code this methods creates a list of AppInfo objects created from the dataframe
def get_app_infos() -> List[AppInfo]:
df = data.query_from_db()
[AppInfo(a) for a in df.to_dict('r')]
Suppose I have a DataFrame including following columns "NAME", "SURNAME", "AGE" and I would like to create one object for each row, including those column values as its variables.
person = ConvertRow2Object(frame.iloc[0,:])
print person.NAME //outputs Gary
How can I do it with a generic solution to any DataFrame with any kind of column names and data types?
You can convert the whole thing to a numpy recarray, then each record in the array is attributed:
people = frame.to_records()
person = people[0]
print person.NAME # etc...
Using a namedtuple also seems to work:
from collections import namedtuple
Person = namedtuple('Person', frame.dtypes.index.tolist())
person = Person(*frame.iloc[0,:])
print person.NAME # etc...
This technique of creating dictionary objects and passing that as init argument worked for me
#AppInfo = Class which I wanted to create instance objects for each row in df
def AppInfo:
def __init__(self, attr_dict):
if attr_dict is not None:
for key, value in attr_dict.items():
setattr(self, key, value)
# in my code this methods creates a list of AppInfo objects created from the dataframe
def get_app_infos() -> List[AppInfo]:
df = data.query_from_db()
[AppInfo(a) for a in df.to_dict('r')]