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 :
- Using eval() function before iloc. This worked. However, eval() being what it is, I threw this alternative away.
- 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]).
If the values of
Ain columnBare values from columnA, then you can split the rows into lists using the '+' separator and take the last elements. Converts the type of columnsA, Cto the 'int64' type. And then perform the operation of adding the values of both columns, overwriting columnC.Or write only the values for addition in column B and perform the addition operation A + B.