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 attribute and make a new column with this value?
Example data:
df
Out[46]:
row custom_object
1 foo1
2 foo2
3 foo3
4 foo4
Name: book, dtype: object
Where the custom objects are of class Foo:
class Foo:
def __init__(self, val):
self.val = val
The only way I know of to create a new column with the instance attributes is using an apply and lambda combo which is slow on large datasets:
df['custom_val'] = df['custom_object'].apply(lambda x: x.val)
Is there a more efficient way?