return floats instead of ndarray in pandas - python -
>>> df=pd.dataframe({'c1':[1,1,1,1,2,2,2,2],'c2':['a','b','a','b','a','a','b','b'],'c3':['w','w','x','x','w','x','w','x'],'c4':[90,28,31,10,21,55,49,23]}) >>> groups = df.groupby(['c1','c3']) >>> groups.apply(lambda x: x[x['c2']=='b'].c4.values / x[x['c2']=='a'].c4.values) c1 c2 1 w [0.31111] x [0.32258] 2 w [2.33333] x [0.41818]
is there way make operation above return float values instead of ndarray?
c1 c2 1 w 0.31111 x 0.32258 2 w 2.33333 x 0.41818
i think can convert output series
:
df = groups.apply(lambda x: pd.series(x[x['c2']=='b'].c4 / x[x['c2']=='a'].c4.values)) .reset_index(level=2, drop=true) print (df) c1 c3 1 w 0.311111 x 0.322581 2 w 2.333333 x 0.418182 name: c4, dtype: float64
Comments
Post a Comment