>>> 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
>>> 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
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: