我具有類似於下面一個大熊貓數據幀:熊貓數據幀:平均值在一個關口,因爲另一個山口具有重複
Time B C
10:06 2 5
10:07 5 3
10:07 4 1
10:08 7 3
10:08 9 4
10:08 5 6
我要平均在列B和C的值,如果「倍」等於。我有下面的代碼,但我意識到我沒有做的時候一般情況下: 1.無重複相同分鐘
內 2.超過3個讀數是否有這樣做的更有效的和廣義方法?
rowF = len(B)
while i < rowF -1:
if (df.loc[i, 'Time'] == df.loc[i+1, 'Time']):
if(df.loc[i,'Time'] == df.loc[i+2, 'Time']):
a = df.loc[i:i+2,'B']
avg = sum(a)/float(len(a))
average.append(avg)
print(average)
else:
a = df.loc[i:i+1,'B']
avg = sum(a)/float(len(a))
average.append(avg)
print(average)
df1['B'] = average
i+=1
預期輸出:
Time B C
10:06 2 5
10:07 4.5 2
10:08 7 4.3
你的電子書是什麼xpected輸出?只需使用'df.groupby('Time',as_index = False).mean()'? – Zero
@零我添加了預期的輸出。感謝您的評論;是否有可能解釋groupby正在做什麼,並且我能否有效地爲大數據集實施它? –
請參閱文檔:https://pandas.pydata.org/pandas-docs/stable/groupby.html – cmaher