【发布时间】:2016-03-03 13:25:37
【问题描述】:
我正在尝试对这些数据进行排序以从中获取:
基本上,我正在尝试将 5 行数据(每行具有 1 个 ID 和 2 个值)压缩为 1 行具有 1 个 ID 和 10 个值的数据。我的数据大约是。 600 万行长。需要注意的一点:并非每个组都有 5 个(X,Y)坐标值。有些只有 4 个。
我无法弄清楚如何仅通过索引来做到这一点。所以我写了一个 for 循环,它不能很好地工作。它将对前 10,000 个 ok 进行排序(但以错误结束),但它需要永远。
coords = pd.read_csv('IDQQCoords.csv')
coords = coords.as_matrix(columns=None)
mpty = np.zeros((len(coords),8),dtype=float)
#creates an empty array the same length as coords
coords = np.append(coords,mpty,axis=1)
# adds the 8 empty columns from the previous command
#This is to make space to add the values from subsequent rows
cnt = 0
lth = coords.shape[0]
for counter in range(1,lth):
if coords[cnt+1,0] == coords[cnt,0]:
coords[cnt,3:5] = coords[cnt+1,1:3]
coords = np.delete(coords,cnt+1,axis=0)
if coords[cnt+1,0] == coords[cnt,0]:
coords[cnt,5:7] = coords[cnt+1,1:3]
coords = np.delete(coords,cnt+1,axis=0)
if coords[cnt+1,0] == coords[cnt,0]:
coords[cnt,7:9] = coords[cnt+1,1:3]
coords = np.delete(coords,cnt+1,axis=0)
if coords[cnt+1,0] == coords[cnt,0]:
coords[cnt,9:11] = coords[cnt+1,1:3]
coords = np.delete(coords,cnt+1,axis=0)
cnt = cnt+1
有人可以帮助我,无论是索引还是更好的循环?
非常感谢
【问题讨论】:
标签: python numpy pandas reshape