How do you calculate expanding mean on time series using pandas?

Posted by mlo on Stack Overflow See other posts from Stack Overflow or by mlo
Published on 2013-10-27T03:41:41Z Indexed on 2013/10/27 3:53 UTC
Read the original article Hit count: 165

Filed under:
|
|
|

How would you create a column(s) in the below pandas DataFrame where the new columns are the expanding mean/median of 'val' for each 'Mod_ID_x'. Imagine this as if were time series data and 'ID' 1-2 was on Day 1 and 'ID' 3-4 was on Day 2.

I have tried every way I could think of but just can't seem to get it right.

left4 = pd.DataFrame({'ID': [1,2,3,4],'val': [10000, 25000, 20000, 40000],'Mod_ID': [15, 35, 15, 42], 
                 'car': ['ford','honda', 'ford', 'lexus']})

right4 = pd.DataFrame({'ID': [3,1,2,4],'color': ['red', 'green', 'blue', 'grey'], 'wheel': ['4wheel','4wheel', '2wheel', '2wheel'], 'Mod_ID': [15, 15, 35, 42]})

df1 = pd.merge(left4, right4, on='ID').drop('Mod_ID_y', axis=1)

Pandas DataFrame

© Stack Overflow or respective owner

Related posts about python-2.7

Related posts about data