为什么不能在该循环之前创建的df.itertuples循环的for x中使用变量?

时间:2019-03-26 15:39:10

标签: python pandas for-loop

这可能是熊猫的基本问题,但我不知道吗?

我有这个循环:

n=0
for lum in lum_df.itertuples():
    print(lum.X)
    print(lum.Y)
    lum_x = float(lum.X)
    lum_y = float(lum.Y)
    for point in street_df.itertuples():
        print(point.X)
        print(point.Y)
        print(lum_x)
        print(lum_y)
        dist = calculate_dist(lum_x, point.X, lum_y, point.Y)
        print('DISTANCE IS : ' + str(dist))
        print('================= next point================')
    print('=============NEXT LUM==============')

以某种方式,当我尝试计算第二个for循环中2个点之间的距离时,值(lum_x和lum_y)作为nan返回。我需要找到一种在第二个循环中使用这些先前创建的变量的方法。为什么它不允许我这样做,我该怎么办?

PS:point.x和point.y已经是float变量!

非常感谢!

2 个答案:

答案 0 :(得分:0)

timeit.timeit('[i for i in (1,3,4,5,6,6,7,78,8)]', number=100000)
0.05304025196801376

答案 1 :(得分:0)

这是lum_df的数据结构:

                    X                 Y          Name
624  617053.712042883  5027348.30038856  AvenuedAnjou
664  617039.606222975    5027394.710913  AvenuedAnjou
692  617033.143825697  5027442.18526991  AvenuedAnjou
723  617024.347605215  5027483.05113423  AvenuedAnjou
751   617006.33108007  5027520.85562618  AvenuedAnjou

,当我运行此变体时:

#Boucle 2
n=0
for ix, lum in lum_df.itertuples():
    print(lum.X)
    print(lum.Y)
    lum_x = float(lum.X)
    lum_y = float(lum.Y)
    distmin=2000000
    nopoint=0
    exit()
    for point in street_df.itertuples():
        print(point.X)
        print(point.Y)
        print(lum_x)
        print(lum_y)
        dist = calculate_dist(lum_x, point.X, lum_y, point.Y)
        print('DISTANCE IS : ' + str(dist))
        print('================= next point================')
    print('=============NEXTLUM==============')

我明白了:

    for ix, lum in lum_df.itertuples():
ValueError: too many values to unpack (expected 2)