如何在 pandas DataFrame中存储公式而不是值 [英] How to store formulas, instead of values, in pandas DataFrame
问题描述
是否可以像处理Excel电子表格一样使用pandas DataFrame:例如,通过在列中输入公式,以便当其他列中的变量发生更改时,该列中的值也会自动更改?像这样:
Is it possible to work with pandas DataFrame as with an Excel spreadsheet: say, by entering a formula in a column so that when variables in other columns change, the values in this column change automatically? Something like:
a b c
2 3 =a+b
因此,当我更新2或3时,列c
也会自动更新.
And so when I update 2 or 3, the column c
also updates automatically.
PS:显然可以编写一个返回a+b
的函数,但是熊猫或其他Python库中是否有任何内置功能可以通过这种方式使用矩阵?
PS: It's clearly possible to write a function to return a+b
, but is there any built-in functionality in pandas or in other Python libraries to work with matrices this way?
推荐答案
这将在0.13(仍在开发中)中工作
This will work in 0.13 (still in development)
In [19]: df = DataFrame(randn(10,2),columns=list('ab'))
In [20]: df
Out[20]:
a b
0 0.958465 0.679193
1 -0.769077 0.497436
2 0.598059 0.457555
3 0.290926 -1.617927
4 -0.248910 -0.947835
5 -1.352096 -0.568631
6 0.009125 0.711511
7 -0.993082 -1.440405
8 -0.593704 0.352468
9 0.523332 -1.544849
这可能是'a + b'
(很快)
In [21]: formulas = { 'c' : 'df.a + df.b' }
In [22]: def update(df,formulas):
for k, v in formulas.items():
df[k] = pd.eval(v)
In [23]: update(df,formulas)
In [24]: df
Out[24]:
a b c
0 0.958465 0.679193 1.637658
1 -0.769077 0.497436 -0.271642
2 0.598059 0.457555 1.055614
3 0.290926 -1.617927 -1.327001
4 -0.248910 -0.947835 -1.196745
5 -1.352096 -0.568631 -1.920726
6 0.009125 0.711511 0.720636
7 -0.993082 -1.440405 -2.433487
8 -0.593704 0.352468 -0.241236
9 0.523332 -1.544849 -1.021517
您可以在数据帧上的 setitem 中实现一个挂钩,以自动调用此类功能.但是非常棘手.您没有首先指定框架的更新方式.更改值后,最简单的调用update函数可能是最简单的
You could implement a hook into setitem on the data frame to have this type of function called automatically. But pretty tricky. You didn't specify how the frame is updated in the first place. Would probably be easiest to simply call the update function after you change the values
这篇关于如何在 pandas DataFrame中存储公式而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!