python - minimize rows with common values, add columns for additional values -
i have array, of following format:
564387.29 7371625.14 0.00 33030.00 -132.96 -1031.50 564387.29 7371625.14 0.00 1530.00 -133.85 -1039.27 564387.29 7371625.14 0.00 47970.00 -138.35 -1044.40 564387.32 7371625.14 0.00 47970.00 -166.41 -999.27 564387.32 7371625.14 0.00 33030.00 -241.74 -1889.71 564387.32 7371625.14 0.00 1530.00 -135.42 -857.31 564387.35 7371625.14 0.00 33030.00 -174.06 -990.66 564387.35 7371625.14 0.00 1530.00 -178.17 -927.11 564387.35 7371625.14 0.00 47970.00 -116.65 -1810.97
i make array pandas dataframe, , sort them based on columns 1, 2 , 4:
564387.29 7371625.14 0.00 1530.00 -133.85 -1039.27 564387.29 7371625.14 0.00 33030.00 -132.96 -1031.50 564387.29 7371625.14 0.00 47970.00 -138.35 -1044.40 564387.32 7371625.14 0.00 1530.00 -135.42 -857.31 564387.32 7371625.14 0.00 33030.00 -241.74 -1889.71 564387.32 7371625.14 0.00 47970.00 -166.41 -999.27 564387.35 7371625.14 0.00 1530.00 -178.17 -927.11 564387.35 7371625.14 0.00 33030.00 -174.06 -990.66 564387.35 7371625.14 0.00 47970.00 -116.65 -1810.97
the final step reduce number of rows adding variable on 4 additional columns following:
564387.29 7371625.14 0.00 1530.00 -133.85 -1039.27 -132.96 -1031.50 -138.35 -1044.40 564387.32 7371625.14 0.00 1530.00 -135.42 -857.31 -241.74 -1889.71 -166.41 -999.27 564387.35 7371625.14 0.00 1530.00 -178.17 -927.11 -174.06 -990.66 -116.65 -1810.97
but cannot seem find way - looked on many numpy , pandas discussion , couldn't ideas
- sort according specific colomns
- group them , apply our customized function
followings example:
in [121]: def func(df): .....: df = df.reset_index(drop=true) .....: s = [df.ix[0][3]] .....: index, rw in df.iterrows(): .....: s.append(rw[4]) .....: s.append(rw[5]) .....: return pd.series(s) .....: in [122]: df.sort([0, 1, 3]).reset_index(drop=true).groupby([0, 1, 2]).apply(func) out[122]: 0 1 2 3 4 5 6 0 1 2 564387.29 7371625.14 0 1530 -133.85 -1039.27 -132.96 -1031.50 -138.35 -1044.40 564387.32 7371625.14 0 1530 -135.42 -857.31 -241.74 -1889.71 -166.41 -999.27 564387.35 7371625.14 0 1530 -178.17 -927.11 -174.06 -990.66 -116.65 -1810.97
Comments
Post a Comment