这应该很容易,但是我无法解决。我有一个带有lon和lat列的数据框,我想列出一个lon和lat值成对的列表。
数据框如下所示:
amenity name lat lon
0 school George Bath Elementary School 40.722452 -75.386650
1 school Dana L Lyon Elementary School 42.337774 -77.317131
2 school Bath Village School 44.168952 -71.963701
3 school Bath School 35.476832 -76.811605
4 school Hyde School 43.905080 -69.822825
我需要一个这样的列表:
[(40.722452, -75.386650), (42.337774, -77.317131), (44.168952, -71.963701)...]
答案 0 :(得分:3)
尝试一下:
list(zip(df['lat'],df['lon']))
答案 1 :(得分:1)
运行:
df[['lat', 'lon']].apply(tuple, axis=1).tolist()
步骤:
df[['lat', 'lon']]
-提取2列有趣的内容,apply(tuple, axis=1)
-将 tuple 函数应用于每一行(到目前为止,
结果是系列),tolist()
-将此系列转换为“普通”列表。答案 2 :(得分:1)
您可以这样操作:
import pandas as pd
#Reading your data
#data -> Pandas dataframe
data = pd.read_excel('yourFile.xlsx')
# Gets the column info
lat = data['lat']
lon = data['lon']
#Process it
result = []
for i in range(len(lat)):
#Adding to result based on indexes
result.append((lat[i], lon[i]))
print result
我试图以一种易于理解的方式编写它(:
答案 3 :(得分:0)
尝试:
df.apply(lambda row: pd.Series([(row['lat'], row['lon'])]), axis=1)[0].tolist()
结果:
[(40.722452000000004, -75.38665),
(42.337773999999996, -77.317131),
(44.168952000000004, -71.963701),
(35.476832, -76.811605),
(43.90508, -69.822825)]