lundi 11 juillet 2016

Pandas mean across multiple columns


I have a dataframe which I created using numpy arrays (MnthIdx, Val1, Val2, Val3) as:

import pandas as pd
import numpy as np

dfout3 = pd.DataFrame({'Idx': MnthIdx,
                       'Col1': Val1,
                       'Col2': Val2,
                       'Col3': Val3)})

MeanTable1 = pd.pivot_table(dfout3, index=['Idx'], values=['Val1'], aggfunc=[np.mean])
MeanVal1 = np.asarray(MeanTable1['mean'])
MeanTable2 = pd.pivot_table(dfout3, index=['Idx'], values=['Val2'], aggfunc=[np.mean])
MeanVal2 = np.asarray(MeanTable2['mean'])
MeanTable2 = pd.pivot_table(dfout3, index=['Idx'], values=['Val3'], aggfunc=[np.mean])
MeanVal3 = np.asarray(MeanTable3['mean'])

Is it possible that I can do the above 3 steps in 1 steps only.


Aucun commentaire:

Enregistrer un commentaire