Skip to content
Advertisement

Factor out the name of the dataframe in python pandas to get better to read mathematical expressions

If you do for example mathematical operations with columns of a python pandas dataframe (call it data), you repeatedly have to write data do access the columns, which is very annoying, if you want nice to read mathematical formulas. So I am looking for a way to “factor out” the data keyword. Consider this simple example:

JavaScript

Where data.dat is

JavaScript

Advertisement

Answer

You can use pandas.DataFrame.eval:

JavaScript

If you want to put all on same line, you can do so:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement