问题
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