Pandas - Fast way of accessing a column of objects' attribute

后端 未结 2 869
囚心锁ツ
囚心锁ツ 2021-01-13 12:29

Let\'s say I have a custom class in python, that has the attribute val. If I have a pandas dataframe with a column of these objects, how can I access this attri

2条回答
  •  执念已碎
    2021-01-13 12:44

    Setup code:

    import operator
    import random
    from dataclasses import dataclass
    
    import numpy as np
    import pandas as pd
    
    
    @dataclass
    class SomeObj:
        val: int
    
    
    df = pd.DataFrame(data={f"col_1": [SomeObj(random.randint(0, 10000)) for _ in range(10000000)]})
    

    Solution 1

    df['col_1'].map(lambda elem: elem.val)
    

    Time: ~3.2 seconds

    Solution 2

    df['col_1'].map(operator.attrgetter('val'))
    

    Time: ~2.7 seconds

    Solution 3

    [elem.val for elem in df['col_1']]
    

    Time: ~1.4 seconds

    Note: Keep in mind that this solution produces a different result type, which may be an issue in certain situations.


提交回复
热议问题