如何在 Pandas DataFrame 中存储公式而不是值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18024742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:03:32  来源:igfitidea点击:

How to store formulas, instead of values, in pandas DataFrame

pythonpandas

提问by Anton Tarasenko

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:

是否可以像使用 Excel 电子表格一样使用 Pandas DataFrame:比如说,通过在列中输入一个公式,以便当其他列中的变量发生变化时,该列中的值会自动更改?就像是:

a  b  c
2  3  =a+b

And so when I update 2 or 3, the column calso updates automatically.

因此,当我更新 2 或 3 时,该列c也会自动更新。

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?

PS:显然可以编写一个函数来 return a+b,但是在 Pandas 或其他 Python 库中是否有任何内置功能可以以这种方式处理矩阵?

回答by Jeff

This will work in 0.13 (still in development)

这将适用于 0.13(仍在开发中)

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

This will be possible as 'a + b'(soon)

这将成为可能'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

You couldimplement a hook into setitemon the data frame to have this type of function called automatically. But pretty tricky. You didn't specify howthe frame is updated in the first place. Would probably be easiest to simply call the update function after you change the values

可以在数据框的setitem 中实现一个钩子,以自动调用这种类型的函数。但相当棘手。没有指定如何框架在首位更新。在更改值后简单地调用更新函数可能是最简单的

回答by jtornero

I don't know it it is what you want, but I accidentally discovered that you can store xlwt.Formula objects in the DataFrame cells, and then, using DataFrame.to_excel method, export the DataFrame to excel and have your formulas in it:

我不知道这是你想要的,但我偶然发现你可以将 xlwt.Formula 对象存储在 DataFrame 单元格中,然后,使用 DataFrame.to_excel 方法,将 DataFrame 导出到 excel 并在其中包含你的公式:

import pandas
import xlwt

formulae=[]
formulae.append(xlwt.Formula('SUM(F1:F5)'))
formulae.append(xlwt.Formula('SUM(G1:G5)'))
formulae.append(xlwt.Formula('SUM(H1:I5)'))
formulae.append(xlwt.Formula('SUM(I1:I5)'))

df=pandas.DataFrame(formula)
df.to_excel('FormulaTest.xls')

Try it...

尝试一下...

回答by Phillip Cloud

There's currently no way to do this exactly in the way that you describe.

目前无法完全按照您描述的方式执行此操作。

In pandas 0.13 there will be a new DataFrame.evalmethod that will allow you to evaluate an expression in the "context" of a DataFrame. For example, you'll be able to df['c'] = df.eval('a + b').

在 pandas 0.13 中,将有一种新DataFrame.eval方法可以让您在DataFrame. 例如,您将能够df['c'] = df.eval('a + b').