Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding calculated column in Pandas

Tags:

python

pandas

I have a dataframe with 10 columns. I want to add a new column 'age_bmi' which should be a calculated column multiplying 'age' * 'bmi'. age is an INT, bmi is a FLOAT.

That then creates the new dataframe with 11 columns.

Something I am doing isn't quite right. I think it's a syntax issue. Any ideas?

Thanks

df2['age_bmi'] = df(['age'] * ['bmi']) print(df2) 
like image 578
JD2775 Avatar asked Jul 29 '17 19:07

JD2775


People also ask

How do I add a calculated column to pandas DataFrame?

To create a new column, use the [] brackets with the new column name at the left side of the assignment.

How can I add sum of column in pandas?

Pandas DataFrame sum() MethodThe sum() method adds all values in each column and returns the sum for each column. By specifying the column axis ( axis='columns' ), the sum() method searches column-wise and returns the sum of each row.

How do you add a column with the same value in a DataFrame?

To add anew column with constant value, use the square bracket i.e. the index operator and set that value.


1 Answers

try df2['age_bmi'] = df.age * df.bmi.

You're trying to call the dataframe as a function, when you need to get the values of the columns, which you can access by key like a dictionary or by property if it's a lowercase name with no spaces that doesn't match a built-in DataFrame method.

Someone linked this in a comment the other day and it's pretty awesome. I recommend giving it a watch, even if you don't do the exercises: https://www.youtube.com/watch?v=5JnMutdy6Fw

like image 188
Cory Madden Avatar answered Sep 20 '22 13:09

Cory Madden