Error:-too many values to unpack (expected 2), while trying to iterate over two columns in a Data Frame

我只是一个虾纸丫 提交于 2019-12-11 07:20:31

问题


for L,M in laundry1['latitude'],laundry1['longitude']:
    print('latitude:-')
    print(L)
    print('longitude:-')
    print(M)

i am trying to iterate over the two columns of a data-frame, assigning there value to L & M and printing there value but it shows error of "too many values to unpack (expected 2) " view of the dataset with error view ->enter image description here

sample output:

latitude:-

22.1449787

18.922290399999998

22.1544736

22.136872

22.173595499999998

longitude:-

-101.0056829

-99.234332

-100.98580909999998

-100.9345736

-100.9946027


回答1:


Use zip:

for L,M in zip(laundry1['latitude'],laundry1['longitude']):
    print('latitude:-')
    print(L)
    print('longitude:-')
    print(M)



回答2:


Pandas has his own iterate methods, if you just want to iterate over a dataframe values, without modifying it, i suggest you to use the itertuples method:

import pandas as pd

values = [[22.1449787,-101.0056829]
         ,[18.922290399999998,-99.234332]
         ,[22.1544736,-100.98580909999998]
         ,[22.136872,-100.9345736]
         ,[22.173595499999998,-100.9946027]]

df = pd.DataFrame(values, columns=['latitude','longitude'])

for row in df.itertuples():
    print(row.latitude)
    print(row.longitude)


来源:https://stackoverflow.com/questions/53778196/error-too-many-values-to-unpack-expected-2-while-trying-to-iterate-over-two

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!