Skip to main content
 首页 » 编程设计

python之使用 Blaze 追加 bcolz 列

2025年05月04日15jackei

让我们首先构建一个ctable:

import pandas as pd 
import blaze as bl 
 
df = pd.DataFrame({'x': range(4), 'y': [2., 4., 2., 4.]}) 
bl.odo(df, 'test.bcolz') 

现在假设我想在这个表中添加一个名为“x_mod”的列。我试过了

test_table = bl.Data('test.bcolz') 
 
def f(h): 
    return h*3 
test_table['x_mod'] = test_table['x'].apply(f, dshape='int64') 
#Or, I think equivalently: 
#test_table['x_mod'] = test_table['x']*3 

但是它给了

TypeError: 'InteractiveSymbol' object does not support item assignment 

1) 如何分配 'x_mod' 列然后保存到磁盘? 我正在处理大型数据库:计算内存中的列应该没问题,但我无法将整个 ctable 加载到内存中。

2) 在相关问题上,apply 对我也不起作用。我做错了什么吗?

#This doesn't work: 
bl.compute(test_table['x'].apply(f, dshape='int64')) 
 
#This I think should be equivalent, but does work: 
bl.compute(test_table['x']*3) 

感谢您的宝贵时间!

请您参考如下方法:

您可以像这样在 Blaze 中使用转换方法:

bz.transform(df, sepal_ratio = df.sepal_length / df.sepal_width   ) 

其他函数需要使用Blaze表达式:

bz.transform(df, sepal_ratio = BLAZE_symbolic_Expression(df.Col1, df.col2)  ) 

它将计算列添加到数据框中。 文档在这里: https://blaze.readthedocs.io/en/latest/expressions.html

例如,您可以使用 map :

from datetime import datetime 
yourexpr = df.col1.map(datetime.utcfromtimestamp) 
bz.transform(df, sepal_ratio=yourexpr)