在 pandas 中添加计算列 [英] Adding calculated column in Pandas

查看:71
本文介绍了在 pandas 中添加计算列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10列的数据框.我想添加一个新列"age_bmi",该列应该是一个乘以"age" *"bmi"的计算列. age是INT,bmi是FLOAT.

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.

然后创建具有11列的新数据框.

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?

谢谢

df2['age_bmi'] = df(['age'] * ['bmi'])
print(df2)

推荐答案

尝试df2['age_bmi'] = df.age * df.bmi.

当您需要获取列的值时,您试图将数据框作为一个函数调用,您可以通过键(如字典)或按属性(如果它是小写的名称,且没有空格,则不能访问)来访问列的值匹配内置的DataFrame方法.

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.

前几天有人在评论中将此链接链接了,这真是太棒了.即使您不进行练习,我也建议您给它一块手表: https://www. youtube.com/watch?v=5JnMutdy6Fw

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

这篇关于在 pandas 中添加计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆