我想在每次遇到nan的时候发布一个新的跑步总量
例如,从所附图片中,它将对前3个值进行求和[1242536, 379759, 1622295]
,然后显示运行总数3244590.0
,然后从第5个值开始计算新的运行总数,直到第9个值,显示这些值的总和,依此类推。我想将这些运行总计放在这些NaN值旁边的新列中
我曾尝试以以下方式处理这个问题:
for i in df['Budget_Expenditure_2012_']:
if np.isnan(i) == True:
x = pd.Index(df['Budget_Expenditure_2012_']).get_loc(i)
print(x)
for item in range(0, len(x) - 1, 2):
second_list.append([x[item],x[item + 1]])
print(second_list)
其思想是找到每对行之间的值之和。这一对将是需要求和的每个范围的起始位置和最后位置。 在这一点上,我不知道如何执行这个求和操作
使用这段代码,您可以在一个名为“总计”的新列上获取每个nan的“运行总计”
使用
shift
、isna
和cumsum
的组合来gropuby
,然后transform
,最后在列为nan
的位置分配结果值相关问题 更多 >
编程相关推荐