我很难理解为什么我的代码无法正常工作。
我有一个dataFame结构化的:屏幕截图到DataFrame(对不起,我没有足够高的声誉来发布图像(
我按如下汇总以获取testBytes
的总和:
aggregation = {'testBytes' : ['sum']}
tests_DL_groupped = tests_DL_short.groupby(['measDay','_p_live','_p_compositeId','Latitude','Longitude','testType']).agg(aggregation).reset_index()
现在,实际问题是为什么此代码无法按预期产生NaN
:
tests_DL_groupped.loc[:,'testMBytes'] = tests_DL_groupped['testBytes']/1000/1000
不工作
虽然此工作正常:
tests_DL_groupped['testMBytes'] = tests_DL_groupped['testBytes']/1000/1000
工作
,这应该是做到这一点的首选方法...
非常感谢!
列中有问题MultiIndex
。
解决方案是变化:
aggregation = {'testBytes' : ['sum']}
to:
aggregation = {'testBytes' : 'sum'}
避免它。
或使用GroupBy.sum
:
cols = ['measDay','_p_live','_p_compositeId','Latitude','Longitude','testType']
tests_DL_groupped = tests_DL_short.groupby(cols)['testBytes'].sum().reset_index()
tests_DL_groupped = tests_DL_short.groupby(cols, as_index=False)['testBytes'].sum()