简体   繁体   English

当我尝试使用 Python Pandas 对行求和时,为什么会显示 NaN?

[英]Why it says NaN when I'm trying to sum rows using Python Pandas?

Title Netquantity Bases Chambers
x          2              4
y                 5       

To get the total of bases chambers, I've used为了得到基地室的总数,我用过

concat_list.loc['Total'] = concat_list[['Bases','Chambers']].sum()

However, the output was然而,output 是

Title Netquantity Bases Chambers

x          2              4
y                 5        
Total    NaN    NaN     NaN

Could you help me to debug this issue?你能帮我调试一下这个问题吗? Some of the numbers are empty.有些号码是空的。 I tried to我尝试过了

concat_list = concat_list.fillna(0)

But still didn't work.但仍然没有工作。

Here is problem there are values mixed strings with numeric or all strings, first tray convert to floats :这里的问题是值混合字符串与数字或所有字符串,第一个托盘转换为floats

concat_list.loc['Total'] = concat_list[['Bases','Chambers']].astype(float).sum()

If not working, try replace not parseable values to missing values by to_numeric per all columns, so use DataFrame.apply :如果不起作用,请尝试将不可解析的值替换为所有列的to_numeric缺失值,因此请使用DataFrame.apply

concat_list.loc['Total'] = concat_list[['Bases','Chambers']].apply(pd.to_numeric, errors='coerce').sum()

暂无
暂无

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

相关问题 我正在尝试使用python pandas数据框将多个列求和成一个新的sum列 - I'm trying to sum multiple columns into a new sum column using a python pandas dataframe 当我尝试使用 Python 连接 Teradata 时,它显示“当前无法通过此网络访问 Teradata 服务器” - It says “The Teradata Server can't currently be reached over this network” when i'm trying to connect Teradata using Python 我正在尝试使用 python 查找前 n 个回文数的总和 - I'm trying to find sum of first n palindromes using python 尝试使用Pandas数据框删除包含nan或inf的行时出现问题 - Issue when trying to remove rows containing nan or inf using Pandas dataframe 我正在尝试使用 Pandas 用 NaN 替换特定列中特定行集中的数据 - I am trying to replace data within a specific set of rows in a specific column with NaN using Pandas 在python中使用Pandas ExcelWriter时处理Nan - Handle Nan when using Pandas ExcelWriter in python 尝试在Pandas数据框中添加列时,为什么会得到np.NaN值? - Why am I getting np.NaN values when trying to add a column to a Pandas dataframe? 当我尝试训练 model 时,输入包含 NaN - input contains NaN when i'm trying to train model Numpy-为什么在尝试删除行时NaN的值错误 - Numpy - why value error for NaN when trying to delete rows 为什么 numpy 在实际尝试转换 int 时告诉我无法转换浮点 NaN? - Why is numpy telling me that I can't convert a float NaN when I'm actually trying to convert an int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM