[英]Sum specific columns if they exist with dataframe
Background背景
I have a dataframe with multiple columns.我有一个多列的 dataframe。 I am trying to sum all the E and B values into a single column我正在尝试将所有 E 和 B 值加到一个列中
t_start t_end B1 E1 B2 E2
1/11/2021 0:00 1/11/2021 0:05 0 2.03 0 9.01
1/11/2021 0:00 1/11/2021 0:05 0 2.03 0 9.01
1/11/2021 0:00 1/11/2021 0:05 0 2.03 0 9.01
Problem问题
Thing is, the file might have 10+ E and B columns, (B1, B2, B3, etc).问题是,该文件可能有 10+ E 和 B 列(B1、B2、B3 等)。
What I've tried我试过的
I've tried the below, but it errors out if the columns don't exist我已经尝试了以下方法,但如果列不存在则会出错
df['sum_b'] = df['b1'] + df['b2'] + df['b3'] ...
I've also tried using a group by solution which I saw here , but it doesn't work either, as it drops off my date columns我也尝试过使用我在这里看到的 group by 解决方案,但它也不起作用,因为它会丢失我的日期列
def left(s, amount):
return s[:amount]
df.T.groupby([left(s, 1) for s in df.T.index.values]).sum().T)
Help Requested请求帮助
If anyone knows how to make a dynamic sum formula which will add b and e columns, it would be much appreciated!如果有人知道如何制作将添加 b 和 e 列的动态求和公式,将不胜感激!
You can use DataFrame.filter(regex=pattern)
, where pattern
is a regular expression designed to pick out your desired columns by name.您可以使用DataFrame.filter(regex=pattern)
,其中pattern
是一个正则表达式,旨在按名称挑选所需的列。
To select columns whose name starts with B
followed immediately by a digit (thus matching B1
, B2
, ..., B10
, and so on):到 select 列,其名称以B
开头,后跟一个数字(因此匹配B1
、 B2
、 ...、 B10
等):
df.filter(regex='^B\d')
To sum the values of all such columns:总结所有这些列的值:
df['sum_b'] = df.filter(regex='^B\d').sum(axis=1)
df['sum_e'] = df.filter(regex='^E\d').sum(axis=1)
df.columns = df.columns.str.replace(r'\s*\d+$', '', regex=True)
new_df = pd.concat([df[['t_start', 't_end']], df.T.drop(['t_start', 't_end']).groupby(level=0).sum().T], axis=1)
Output: Output:
>>> new_df
t_start t_end B E
0 1/11/2021 0:00 1/11/2021 0:05 0.0 11.04
1 1/11/2021 0:00 1/11/2021 0:05 0.0 11.04
2 1/11/2021 0:00 1/11/2021 0:05 0.0 11.04
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.