[英]Sum All Positive and All Negative Values Pandas
I have the following dataframe (df):我有以下 dataframe (df):
Row Number
Row 0 0.24 0.16 -0.18 -0.20 1.24
Row 1 0.18 0.12 -0.73 -0.36 -0.54
Row 2 -0.01 0.25 -0.35 -0.08 -0.43
Row 3 -0.43 0.21 0.53 0.55 -1.03
Row 4 -0.24 -0.20 0.49 0.08 0.61
Row 5 -0.19 -0.29 -0.08 -0.16 0.34
I am attempting to sum all the negative and positive numbers respectively, eg sum(neg_numbers) = n and sum(pos_numbers) = x我试图分别对所有负数和正数求和,例如 sum(neg_numbers) = n 和 sum(pos_numbers) = x
I have tried:我努力了:
df.groupby(df.agg([('negative' , lambda x : x[x < 0].sum()) , ('positive' , lambda x : x[x > 0].sum())])
to no avail.无济于事。
How would I sum these values?我将如何总结这些价值?
Thank you in advance!先感谢您!
You can do 你可以做
sum_pos = df[df>0].sum(1)
sum_neg = df[df<0].sum(1)
if you want to get the sums per row. 如果要获取每行的总和。 If you want to sum all values regardless of rows/columns, can use
np.nansum
如果要对所有值求和而不管行/列如何,可以使用
np.nansum
sum_pos = np.nansum(df[df>0])
You can do with 你可以做
df.mul(df.gt(0)).sum().sum()
Out[447]: 5.0
df.mul(~df.gt(0)).sum().sum()
Out[448]: -5.5
If need sum by row 如果需要逐行求和
df.mul(df.gt(0)).sum()
Out[449]:
1 0.42
2 0.74
3 1.02
4 0.63
5 2.19
dtype: float64
Yet another way for the total sums: 总和的另一种方式:
sum_pos = df.to_numpy().flatten().clip(min=0).sum()
sum_neg = df.to_numpy().flatten().clip(max=0).sum()
And for sums by columns: 对于按列求和:
sum_pos_col = sum(df.to_numpy().clip(min=0))
sum_neg_col = sum(df.to_numpy().clip(max=0))
If you have string columns in dataframe and want to get the sum for particular column, then如果您在 dataframe 中有字符串列并且想要获取特定列的总和,那么
df[df['column_name']>0]['column_name'].sum()
df[df['column_name']<0]['column_name'].sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.