Create a new column based on calculations that change between rows?


I would like to calculate a sum of variables for a given day. Each day contains a different calculation, but all the days use the variables consistently.

There is a df which specifies my variables and a df which specifies how calculations will change depending on the day.

How can I create a new column containing answers from these different equations?

import pandas as pd import numpy as np conversion = [["a",5],["b",1],["c",10]] conversion_table = pd.DataFrame(conversion,columns=['Variable','Cost']) data1 = [[1,"3a+b"],[2,"c"],[3,"2c"]] to_solve = pd.DataFrame(data1,columns=['Day','Q1']) desired = [[1,16],[2,10],[3,20]] desired_table=pd.DataFrame(desired,columns=['Day','Q1 solved'])

I have separated my variables and equations based on row. Can I loop though these equations to find non-numerics and re-assign them?

#separate out equations and values for var in conversion_table["Variable"]: cost=(conversion_table.loc[conversion_table['Variable'] == var, 'Cost']).mean() for row in to_solve["Q1"]: equation=row

A simple suggestion, perhaps you need to rewrite a part of your code. Not sure if your want something like this:

a = 5 b = 1 c = 10 # Rewrite the equation that is readable by Python # e.g. replace 3a+b by 3*a+b data1 = [[1,"3*a+b"], [2,"c"], [3,"2*c"]] desired_table = pd.DataFrame(data1, columns=['Day','Q1']) desired_table['Q1 solved'] = desired_table['Q1'].apply(lambda x: eval(x)) desired_table


Day Q1 Q1 solved 0 1 3*a+b 16 1 2 c 10 2 3 2*c 20

If it's possible to have the equations changed to equations with * then you could do this.

Get the mapping from the

mapping = dict(zip(conversion_table['Variable'], conversion_table['Cost'])

the eval the function and replace variables with numeric from the mapping

desired_table['Q1 solved'] = to_solve['Q1'].map(lambda x: eval(''.join([str(mapping[i]) if i.isalpha() else str(i) for i in x]))) 0 16 1 10 2 20



  • Create a new column based on calculations that change between rows?
  • Service worker - network first then cache with fallback to static page
  • How to use Java DeflaterOutputStream
  • DateTime string format last digit of year
  • Meteor load scripts, CSS specific to pages
  • Adding a column before another one in Rails
  • AWS Trust Policy Has prohibited field Principal
  • How to check that variable is a lambda function
  • Custom string Comparison in C#
  • Converting SVG with Embedded CSS to PDF in Python
  • VBA Excel accessing index with named range [closed]
  • Need PowerShell Script in NuGet to install selected DLLs from Package into a VS Project
  • opencv error: assertion failed (size.width>0 && size.height>0) in unknown function lin
  • Outlook PropertyFrom MAPI Schema Property ID
  • java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.gra
  • how come BinaryFormatter can serialize an Action but Json.net cannot
  • Dynamically change branches on AWS CodePipeline
  • GraphicsMagick for node not masking
  • unable to set Row.Readonly=false in Datagridview in winforms
  • Get a class as javax.lang.model.element.Element in junit tests
  • Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder
  • UIViewControllerAnimatedTransitioning: Black screen fragments after rotation change
  • Login working for Iphone but not android
  • How to parse JSON in Spring Boot Application automatically using Jackson
  • Tomcat Replacing VM arguments
  • PushPlugin not found, or is not a CDVPlugin
  • how to ignore files when finishing private ClearCase branch?
  • hadoop jar command points to local filesystem
  • Converting array with Different data type
  • How to set a dynamic form fields to dirty or touched with angular?
  • Project Euler -Prob. #20 (Lua)
  • Binding json result in highcharts for asp.net mvc 4
  • PHP users local time
  • Create One-To-One relationship between table and view in EF4?
  • How to make a dependent dropdown in codeigniter
  • python selenium scraping tbody
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Thumbnails for mxml components in Flex
  • PHPMailer return to AJAX
  • media foundation H264 decoder not working properly