Skip to content Skip to sidebar Skip to footer

Convert Dataframe Columns To Objects Efficiently

I have a Panda's DataFrame with a lot of entries. I would like to create an object by row without doing an iteration if possible. Ie: Age Name ----------- 0 1

Solution 1:

If you want a list of objects, you can use itertuples, these return namedtuple objects (well, almost).

list(df.itertuples(name='Person', index=False))

[Person(Age=13, Name='John'),
 Person(Age=16, Name='Marc'),
 Person(Age=17, Name='Prisl'),
 Person(Age=14, Name='Mike'),
 Person(Age=11, Name='Robert')]

Another idea uses namedtuple and apply.

from collections import namedtuple
cls = namedtuple(typename='Person', field_names=df.columns.tolist())

df.apply(lambda r: cls(**r.to_dict()), 1)

0      (13, John)
1      (16, Marc)
2     (17, Prisl)
3      (14, Mike)
4    (11, Robert)
dtype: object

df.apply(lambda r: cls(**r.to_dict()), 1).tolist()

[Person(Age=13, Name='John'),
 Person(Age=16, Name='Marc'),
 Person(Age=17, Name='Prisl'),
 Person(Age=14, Name='Mike'),
 Person(Age=11, Name='Robert')]

If you aren't particular on classes, you can use to_dict to return a list of records.

df.to_dict('records')

[{'Age': 13, 'Name': 'John'},
 {'Age': 16, 'Name': 'Marc'},
 {'Age': 17, 'Name': 'Prisl'},
 {'Age': 14, 'Name': 'Mike'},
 {'Age': 11, 'Name': 'Robert'}]

Post a Comment for "Convert Dataframe Columns To Objects Efficiently"