Call a variable from a str

129 Views Asked by At

I have an Excel spreadsheet - that I read via pandas - with some values corresponding to A + x ; A being an already defined value and x being an int. This Excel is a list of subjects with each subject having a different value for A and B.

I technically found a solution, but would like to know if there are alternative solutions to what I did. The Excel file being massive, I'd like a solution that doesn't require to write hundreds of lines.

ID A B
01 5 A+2
02 8 A+4

Consequently, something like "A + 2" will be listed as str in the pandas DataFrame I get from pd.read_excel.

What I want is to make a DataFrame of, let's say, 10 entries with each entry being either ID 01 or ID 02 randomly selected.

Let's say I select the first line, with A = 5 defined before reading B. I'd like to get B = 7 by reading the corresponding entry in pandas. This would normally be obtainable via DF.iloc[0,1]. However, A + 2 being str, I can't.

I found two potent solutions :

  1. Using eval() function before iloc. This worked. However, eval() being what it is, I threw this alternative away.
  2. Using locals() and a function like the following one:
def numeric(equation):
   A = DF.iloc[0,0]
   if '+' in equation:
      x = equation.split('+')
      y = locals()[x[0]]+int(x[1])
      return y

Then using numeric(DF.iloc[0,1]).

1

There are 1 best solutions below

0
inquirer On

If the values of A in column B are values from column A, then you can split the rows into lists using the '+' separator and take the last elements. Converts the type of columns A, C to the 'int64' type. And then perform the operation of adding the values of both columns, overwriting column C.

Or write only the values for addition in column B and perform the addition operation A + B.

import pandas as pd


df = pd.DataFrame({'ID': ['01', '02'], 'A': [5, 8], 'B': ['A+2', 'A+4']})
df['C'] = df['B'].str.split('+').str[1]

df = df.astype({'A': 'int64', 'C': 'int64'})

df['C'] = df['A'] + df['C']