繁体   English   中英

对 Pandas DataFrame 中存在非数字值的所有列求和

[英]Sum all columns in a Pandas DataFrame where there are non-numeric values

我有以下数据集:

df = pd.DataFrame({'col1' : [12,3,4,5,'a',5], 'col2' : [1,5,'b',6,10,1]})

如果我运行df.sum(axis=0, numeric_only=True) ,我会得到以下输出:

Series([], dtype: float64)

但是,如果我将非数字值更改为None则它工作正常。

所以,我的问题是,当存在非数字值时,如何找到数据集中所有列的总和?

我认为您可以将to_numericapply一起apply因为to_numeric仅适用于列( Series ):

print (df.apply(pd.to_numeric, errors='coerce').sum())
#same as
#print (df.apply(lambda x: pd.to_numeric(x, errors='coerce')).sum())
col1    29.0
col2    23.0
dtype: float64

另一个解决方案是concatlist comprehension

df = pd.concat([pd.to_numeric(df[col], errors='coerce') for col in df], axis=1).sum()
print (df)
col1    29.0
col2    23.0
dtype: float64

如果只有几列更快是重复代码:

df.col1 = pd.to_numeric(df.col1, errors='coerce')
df.col2 = pd.to_numeric(df.col2, errors='coerce')
print (df.sum())
col1    29.0
col2    23.0
dtype: float64

我认为numeric_only=True不适用于混合内容的列 - 带有字符串值的数字。

示例 - col1是数字, col2是非数字:

df = pd.DataFrame({'col1' : [1,3,4], 'col2' : ['1','5','b']})
print (df)
   col1 col2
0     1    1
1     3    5
2     4    b

print (df.sum(numeric_only=True))
col1    8
dtype: int64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM