pandas :特定栏目的产品 [英] Pandas: Product of specific columns

查看:60
本文介绍了 pandas :特定栏目的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据框中查找所有列的乘积很容易:

Finding the product of all columns in a dataframe is easy:

df['Product'] = df.product(axis=1)

如何指定要在产品操作中包括的列名(不是列号)?

How can I specify which column names (not column numbers) to include in the product operation?

DataFrame.product()的帮助页面中,我不确定是否可以.

From the help page for DataFrame.product(), I am not sure whether it is possible.

推荐答案

您可以使用df[[colname1, colname2, colname3...]]语法选择所需的列,然后在其上调用.product:

You can use the df[[colname1, colname2, colname3...]] syntax to select the columns you want and then call .product on that:

>>> df = pd.DataFrame({"A": [2,2], "B": [3,3], "C": [5,5]})
>>> df
   A  B  C
0  2  3  5
1  2  3  5

[2 rows x 3 columns]
>>> df[["A", "C"]].product(axis=1)
0    10
1    10
dtype: int64

这篇关于 pandas :特定栏目的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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