Python – Is there a way to avoid typing the data frame name, parentheses, and quotes when creating a new column in a Python/Pandas data frame?

Is there a way to avoid typing the data frame name, parentheses, and quotes when creating a new column in a Python/Pandas data frame?… here is a solution to the problem.

Is there a way to avoid typing the data frame name, parentheses, and quotes when creating a new column in a Python/Pandas data frame?

Let’s say I have a Python/Pandas data frame named df1 with columns a and b, with only one record per column (a = 1 and b = 2). I want to create a third column c whose value is equal to a + b or 3.

With Pandas, I would write:

df1['c'] = df1['a'] + df1['b'] 

I’d rather just write something simpler and easier to read, like this:

with df1:
    c = a + b

SAS allows this simpler syntax in its Data Steps. I would be happy if Python/Pandas had something similar.

Thank you so much!
Sean

Solution

Use the DataFrame.eval() method:

Demo:

In [17]: df = pd. DataFrame({'a':[1], 'b':[2]})

In [18]: df
Out[18]:
   a  b
0  1  2

In [19]: df.eval("c = a + b", inplace=True)

In [20]: df
Out[20]:
   a  b  c
0  1  2  3

Related Problems and Solutions