有效地将DataFrame列转换为对象 [英] Convert DataFrame columns to Objects efficiently

查看:650
本文介绍了有效地将DataFrame列转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有很多条目的熊猫数据框。如果可能,我想逐行创建对象。



即:

 年龄
-----------
0 13 John
1 16 Marc
2 17 Prisl
3 14 Mike
4 11 Robert

有可能吗?在文档中我对此一无所获。

解决方案

如果要对象列表,可以使用 itteruples ,它们返回 namedtuple 对象(好了,差不多)。

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

[Person(Age = 13,Name ='John'),
Person(年龄= 16,姓名='马克'),
Person(年龄= 17,姓名='Prisl'),
Person(年龄= 14,姓名='麦克'),
Person(Age = 11,Name ='Robert')]






另一个想法是使用 namedtuple apply



<从集合中导入pre> 导入namedtuple
cls = namedtuple(typename ='Person',field_names = df.columns.tolist())

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

0(13,约翰)
1(16,马克)
2(17,Prisl )
3(14,迈克)
4(11,罗伯特)
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')]






如果您对课堂并不特别,可以使用 to_dict 返回记录列表。

  df.to_dict('records')

[{年龄':13,'姓名':'约翰'},
{'年龄':16,'姓名':'马克'},
{'年龄':17,'姓名':' Prisl'},
{'Age':14,名称:'Mike'},
{'Age':11,'Name':'Robert'}]


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   13   John
    1   16   Marc
    2   17   Prisl
    3   14   Mike
    4   11   Robert

Is that possible? I found nothing on the documentation regarding this.

解决方案

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'}]

这篇关于有效地将DataFrame列转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆