使用 Python Pandas 使用通配符名称搜索对所有列求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12569730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:26:16  来源:igfitidea点击:

Sum all columns with a wildcard name search using Python Pandas

pythonwildcardpandas

提问by jbssm

I have a dataframe in python pandas with several columns taken from a CSV file.

我在 python pandas 中有一个数据框,其中有几列取自 CSV 文件。

For instance, data =:

例如,数据=:

Day P1S1 P1S2 P1S3 P2S1 P2S2 P2S3
1   1    2    2    3    1    2
2   2    2    3    5    4    2

And what I need is to get the sum of all columns which name starts with P1... something like P1* with a wildcard.

而我需要的是获取名称以 P1 开头的所有列的总和......类似于带有通配符的 P1* 。

Something like the following which gives an error:

类似于以下内容的内容会出现错误:

P1Sum = data["P1*"]

P1Sum = 数据["P1*"]

Is there any why to do this with pandas?

有什么理由对Pandas这样做吗?

回答by jbssm

I found the answer.

我找到了答案。

Using the data, dataframe from the question:

使用数据,问题中的数据框:

from pandas import *

P1Channels = data.filter(regex="P1")
P1Sum = P1Channels.sum(axis=1)

回答by Anton Tarasenko

List comprehensions on columns allow more filters in the ifcondition:

列上的列表推导式允许在if条件中使用更多过滤器:

In [1]: df = pd.DataFrame(np.arange(15).reshape(5, 3), columns=['P1S1', 'P1S2', 'P2S1'])

In [2]: df
Out[2]: 
   P1S1  P1S2  P2S1
0     0     1     2
1     3     4     5
2     6     7     8
3     9    10    11
4    12    13    14

In [3]: df.loc[:, [x for x in df.columns if x.startswith('P1')]].sum(axis=1)
Out[3]: 
0     1
1     7
2    13
3    19
4    25
dtype: int64

回答by jarvis

Thanks for the tip jbssm, for anyone else looking for a sum total, I ended up adding .sum()at the end, so:

感谢 jbssm 的提示,对于其他寻找总和的人,我最终添加.sum()了,所以:

P1Sum= P1Channels.sum(axis=1).sum()