I have a column of type Object, and I want to access a property of the object into another column.
import pandas as pd
class foo(object):
@property
def value(self):
return "bar"
if __name__ == "__main__":
a = [foo(), foo(), foo()]
df = pd.DataFrame(data=a, columns=['test'])
df['value'] = df['test'].value
This fails with the following error:
AttributeError: 'Series' object has no attribute 'value'
Is there a way to call a property or function on a class to populate a new column?
class foo(object):
@property
def value(self):
return "bar"
if __name__ == "__main__":
a = [foo(), foo(), foo()]
df = pd.DataFrame(data=a, columns=['test'])
df['value'] = df['test'].apply(lambda x: x.value)
df
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With