Averaging Pandas DataFrames

This is the place for queries that don't fit in any of the other categories.

Averaging Pandas DataFrames

Postby tnknepp » Mon Sep 23, 2013 2:03 pm

I want to extract averaged data from a Pandas.DataFrame, but hit a snag due to the "average" being transformed from a DataFrame to a Series. e.g.

Code: Select all
import numpy as np
import pandas as pd

a = pd.DataFrame(np.random.rand(5,5),columns=list('ABCDE'))
b = a.mean(axis=0)

>>> b
A    0.536495
B    0.522431
C    0.582918
D    0.600779
E    0.371422
dtype: float64

My application is to take the averaged values and insert them into another dataframe, e.g.

Code: Select all
# Average parameters collected when "hour of day" (data.hod) is 13
tmp = pd.DataFrame()
tmp = pd.concat( [tmp,data[data.hod==13].mean(axis=0)], axis=0)

This is where it gets ticked off and gives the AttributeError: 'Series' object has no attribute '_data'

I would expect there exists some way of averaging a dataframe that does not involve converting the output to a Series. I know this is doable when the dataframe is multi-indexed, but that is not done here. Does anyone know how to perform this operation?
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests