1
>>> df['X'].head() 0 25+4 1 25+5 2 15+3 3 20+2 4 20+3 Name: X, dtype: object 

How do I evaluate this so my dataframe is this:

>>> df['X'].head() 0 29 1 30 2 18 3 22 4 23 Name: X, dtype: int64 
1
  • 1
    A word of warning: evaling arbitrary Python code is potentially dangerous, especially if the code comes from an external source that you cannot 100% trust. Remember Little Bobby Tables. Commented May 7, 2016 at 2:59

1 Answer 1

1

Although there are security concerns, you can use eval to evaluate each element using a lambda expression.

df = pd.DataFrame({'X': ['25+4', '25+5', '15+3', '20+2', '20+3']}) >>> df X 0 25+4 1 25+5 2 15+3 3 20+2 4 20+3 >>> df.X.apply(lambda x: eval(x)) 0 29 1 30 2 18 3 22 4 23 Name: X, dtype: int64 

For a description of security concerns, see:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.