I have a very long list of strings. These strings are those columns I would like to keep from a dataframe. However, there are a couple of strings in this list that don't match a column header, instead it matches an operation between two or more column headers I would like to performs.
Example:
df = @A @B @C @D @E @F
1 2 3 4 5 6
9 8 7 6 5 4
1 3 5 7 9 11
list = ["@A", "@C", "@D / @F"]
I would like this to output:
@A @C @D/@F
1 3 0.667
9 7 1.5
1 5 0.63
However, I can't for the life of me figure out what to do.
To make things a bit more complicated, sometimes in my list you might see a simple division like in my example, sometimes you might see something more complicated like "(C + D) / (A + F)"
. How would I do this??
I honestly can't figure out where to start. I thought I could just do something like df[list] and pandas would just "know" what to do, but I think I might be asking a bit too much!
Any help would be appreciated! Thanks.
Edit: After trying to use the eval() method, I've bumped into another issue. I didn't though this could be a problem, but most of my strings begin with the character "@" which throws up an error when passed into the eval function. I'll do some googling on how to work around this, but any additional help is also welcome.
Edit 2:
I figured out a work around for the @ issue. I simply used the replace() method.
lst = [word.replace("@", "") for word in lst]
Then, I do the same this for all of my columns as well.