gpt4 book ai didi

python - 行中列表的元素

转载 作者:行者123 更新时间:2023-12-01 03:08:57 24 4
gpt4 key购买 nike

我有几个多边形以及一些点与这些多边形的距离。我尝试用 pandas 编写 csv,其中每个点和多边形之间的距离将出现在单独的行中。我得到了这个:

poly total inside outside dist
1000 2 0 2 [16015,5678]
1100 1 0 1 [5267]

我想要这样:

poly total inside outside dist
1000 2 0 2 16015
1000 2 0 2 5678
1100 1 0 1 5267

在查看了之前的问题后,我尝试了以下操作[ How to write nth value of list into csv file

distance =[]
for row in arcpy.da.SearchCursor(outSide, ["SHAPE@XY"]):
px, py = row[0]
zipPoint=point(px,py)
Distance.append(int(zipPoint.calDist(PolyCenter)))
for i in distance:
df.loc[polygon,"distance"]=distance
df.loc[zipCode,"Total"]=count
df.loc[zipCode,"Inside"]=insideNum
df.loc[zipCode,"Outside"]=outsideNum

但它在 csv 中给了我相同的结果。如有任何帮助,我们将不胜感激。

最佳答案

您可以使用str.len获取由numpy.repeat重复的列表的长度与 flattening lists然后join原始栏目:

from  itertools import chain

s = pd.Series(list(chain.from_iterable(df.dist)),
index=np.repeat(df.index.values, df.pop('dist').str.len())).rename('dist')
print (s)
0 16015
0 5678
1 5267
Name: dist, dtype: int64

print (df.join(s).reset_index(drop=True))
poly total inside outside dist
0 1000 2 0 2 16015
1 1000 2 0 2 5678
2 1100 1 0 1 5267

另一个解决方案 MultiIndex :

names = ['poly','total', 'inside','outside']
df = df.set_index(names)
mux = pd.MultiIndex.from_tuples(np.repeat(df.index.values, df.dist.str.len()), names=names)
df2 = pd.DataFrame({'dist':list(chain.from_iterable(df.dist))}, index=mux).reset_index()
print (df2)
poly total inside outside dist
0 1000 2 0 2 16015
1 1000 2 0 2 5678
2 1100 1 0 1 5267

关于python - 行中列表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43084260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com