我有一个阵列,格式如下:
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
我将数组作为pandas数据帧,并根据第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
最后一步是通过在另外4列上添加变量来减少行数,以获得以下内容:
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
但我似乎无法找到方法 - 我看了许多numpy和pandas讨论并且无法得到任何想法
答案 0 :(得分:2)
以下是示例:
In [121]: def func(df):
.....: df = df.reset_index(drop=True)
.....: s = [df.ix[0][3]]
.....: for 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
答案 1 :(得分:0)
564387.29 7371625.14 0 0 1530.00000
1 -133.84641
2 -1039.27338
3 -133.48476
4 -1036.76991
5 -307.65603
6 -1747.01828
7 -132.95704
8 -1031.49587
9 -138.35364
10 -1044.39817
11 -119.70329
12 -981.84015
13 -89.65443
14 -919.83924
...
它看起来像分组的所有工作,然后行不会被追加......