Fill in missing pandas data with previous non-missing value, grouped by key

You could perform a groupby/forward-fill operation on each group: import numpy as np import pandas as pd df = pd.DataFrame({‘id’: [1,1,2,2,1,2,1,1], ‘x’:[10,20,100,200,np.nan,np.nan,300,np.nan]}) df[‘x’] = df.groupby([‘id’])[‘x’].ffill() print(df) yields id x 0 1 10.0 1 1 20.0 2 2 100.0 3 2 200.0 4 1 20.0 5 2 200.0 6 1 300.0 7 1 300.0

Python pandas groupby aggregate on multiple columns, then pivot

df.groupby(‘Category’).agg({‘Item’:’size’,’shop1′:[‘sum’,’mean’,’std’],’shop2′:[‘sum’,’mean’,’std’],’shop3′:[‘sum’,’mean’,’std’]}) Or if you want it across all shops then: df1 = df.set_index([‘Item’,’Category’]).stack().reset_index().rename(columns={‘level_2′:’Shops’,0:’costs’}) df1.groupby(‘Category’).agg({‘Item’:’size’,’costs’:[‘sum’,’mean’,’std’]})

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)