【发布时间】:2021-10-26 05:38:57
【问题描述】:
我想做这个
| City |
|---|
| Seoul |
| Busan |
| Deagu |
| Seoul |
| Seoul |
| Busan |
进入这个现在有latitude 和longtitue
| City | Latitude | Logitude |
|---|---|---|
| Seoul | 127 | 50 |
| Busan | 128 | 51 |
| Daegu | 129 | 52 |
| Seoul | 127 | 50 |
| Seoul | 127 | 50 |
| Busan | 128 | 51 |
我用下面的代码试过了
import pandas as pd
dict1 = {"Seoul":[127,50],
"Busan":[128,51],
"Daegu":[129,52]}
data = {'City':['Seoul', 'Busan', 'Daegu', 'Seoul','Seoul','Busan']}
test1 = pd.DataFrame(data=data)
for city, list1 in dict1.items():
print(city," ", list1[0],list1[1])
for num, city, list1 in zip(range(7),dict1.items()):
if test1.loc[:,"City"] == city:
test1.loc["Latitude"] = list1[0]
test1.loc["Longitude"] = list1[1]
但它返回错误,因为没有足够的元素用于for loop
并且需要比迭代列表长度更有效的方法
祝你有美好的一天!
【问题讨论】:
标签: python pandas numpy-ndarray