假设我在 python 中有一个自定义类,它具有属性 val。如果我有一个包含这些对象列的 pandas 数据框,我如何访问此属性并使用此值创建一个新列?
示例数据:
df
Out[46]:
row custom_object
1 foo1
2 foo2
3 foo3
4 foo4
Name: book, dtype: object
自定义对象属于 Foo 类:
class Foo:
def __init__(self, val):
self.val = val
我知道使用实例属性创建新列的唯一方法是使用 apply 和 lambda 组合,这在大型数据集上很慢:
df['custom_val'] = df['custom_object'].apply(lambda x: x.val)
有没有更高效的方法?
请您参考如下方法:
您可以使用列表理解:
df['custom_val'] = [foo.val for foo in df['custom_object']]
时间
# Set-up 100k Foo objects.
vals = [np.random.randn() for _ in range(100000)]
foos = [Foo(val) for val in vals]
df = pd.DataFrame(foos, columns=['custom_object'])
# 1) OP's apply method.
%timeit df['custom_object'].apply(lambda x: x.val)
# 10 loops, best of 3: 26.7 ms per loop
# 2) Using a list comprehension instead.
%timeit [foo.val for foo in df['custom_object']]
# 100 loops, best of 3: 11.7 ms per loop
# 3) For reference with the original list of objects (slightly faster than 2) above).
%timeit [foo.val for foo in foos]
# 100 loops, best of 3: 9.79 ms per loop
# 4) And just on the original list of raw values themselves.
%timeit [val for val in vals]
# 100 loops, best of 3: 4.91 ms per loop
如果您有原始的值列表,您可以直接分配它们:
# 5) Direct assignment to list of values.
%timeit df['v'] = vals
# 100 loops, best of 3: 5.88 ms per loop
