[英]Pandas .sum() refuses to sum column properly
I have a bit of code here that is doing a few things, but most importantly summing a single particular column in a dataset. 我这里有一些代码在做一些事情,但最重要的是汇总数据集中的单个特定列。
# Limit the values to be summed based on the IDL
for i in range(len(merge1)):
print (" Checking Value Against IDL ")
if float(merge1.iloc[i, 8]) <= float(merge1.iloc[i, 3]):
merge1.iloc[i, 8] = 0
###
merge1.columns =('CB #', 'PCB Homolog Group', 'Ref #', 'IDL ng mL', 'Target #', 'CB #_2', 'R.T. min', 'Response Sig.', 'Concentration', 'Units')
columns = merge1.columns
print (columns)
merge1.at['Total', 'Concentration'] = merge1.iloc[:, 8].sum()
print (merge1)
merge1.to_csv( CBReference_Dir + 'Export_Test_3.csv', index = None, header = True)
The .sum()
should now sum the entire contents of the column at index position #8, but instead this is happening: .sum()
现在应该将索引位置8处的列的所有内容相加,但是发生了:
All of the contents of each row in that column are simply being placed next to one-another in a single cell which I specified. 在我指定的单个单元格中,该列中每一行的所有内容都简单地彼此相邻放置。 They should be summed instead and the total should be around ~85. 相反,应将它们相加,总数应约为85。 I'm lost and not sure how to get this column to sum properly, I have never run into this issue with pandas dataframes before... 我迷路了,不确定如何正确地汇总此列,之前我从未遇到过有关熊猫数据框的问题...
A usable solution was: 可用的解决方案是:
merge1.at['Total', 'Concentration'] = sum(map(float, merge1['Concentration']))
print (merge1)
Using the map command I was able to apply the float
class to each position in the column I desired to sum. 使用map命令,我能够将float
类应用于我希望求和的列中的每个位置。 From there, everything in that column was summed as expected. 从那里开始,该列中的所有内容均按预期进行汇总。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.