我使用以下代码填充NaN
值,然后向DataFrame
添加一列,其中包含一行中大于0的值的数量。这里'代码:
df.fillna(0, inplace=True)
dfMin10 = df
dfMin10['Sum'] = (dfMin10.iloc[1:len(dfMin10.columns)] > 0).sum(1)
dfMin10
当我看到专栏Sum
时,我仍会看到一些NaN
值。为什么会这样?我假设我的DataFrame (df)
在替换NaN
后也有一些NaN
值。
任何指针都会受到高度赞赏。
答案 0 :(得分:3)
您是否在第一个NaN
条目中看到了sum
?这一行:
branchConceptsWithScoresMin10['Sum'] = (branchConceptsWithScoresMin10.iloc[1:len(branchConceptsWithScoresMin10.columns)] > 0).sum(1)
应该是:
branchConceptsWithScoresMin10['Sum'] = (branchConceptsWithScoresMin10.iloc[0:len(branchConceptsWithScoresMin10.columns)] > 0).sum(1)
请注意从0
开始的索引。
示例强>:
df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z'])
df.fillna(0, inplace=True)
branchConceptsWithScoresMin10 = df
# Your original code
branchConceptsWithScoresMin10['Sum'] = (branchConceptsWithScoresMin10.iloc[1:len(branchConceptsWithScoresMin10.columns)] > 0).sum(1)
# This should return
# a b c d Sum
# x 0 0 0 0 NaN
# y 0 0 0 0 0.0
# z 0 0 0 0 0.0
branchConceptsWithScoresMin10['Sum'] = (branchConceptsWithScoresMin10.iloc[0:] > 0).sum(1)
# There should not be any NaNs here.