Python 从两个dataframe列创建值对列表

Python 从两个dataframe列创建值对列表,python,pandas,list,dataframe,Python,Pandas,List,Dataframe,这应该很容易,但我就是做不出来。我有一个带有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

这应该很容易,但我就是做不出来。我有一个带有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)...]
我需要这样一份清单:

    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)...]
尝试:

结果:

[(40.722452000000004, -75.38665),
 (42.337773999999996, -77.317131),
 (44.168952000000004, -71.963701),
 (35.476832, -76.811605),
 (43.90508, -69.822825)]
尝试:

结果:

[(40.722452000000004, -75.38665),
 (42.337773999999996, -77.317131),
 (44.168952000000004, -71.963701),
 (35.476832, -76.811605),
 (43.90508, -69.822825)]
运行:

步骤:

  • df[['lat','lon']]
    -选取两列感兴趣的内容
  • apply(tuple,axis=1)
    -将tuple函数应用到每一行(到目前为止) 结果是一个系列)
  • tolist()
    -将此系列转换为“普通”列表
    • 运行:

      步骤:

      • df[['lat','lon']]
        -选取两列感兴趣的内容
      • apply(tuple,axis=1)
        -将tuple函数应用到每一行(到目前为止) 结果是一个系列)
      • tolist()
        -将此系列转换为“普通”列表
      试试这个:

      list(zip(df['lat'],df['lon']))
      
      试试这个:

      list(zip(df['lat'],df['lon']))
      
      您可以这样做:

       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
      

      我试着用一种容易理解的方式写它(:

      你可以这样做:

       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
      

      我试着用一种简单易懂的方式写它(:

      这回答了你的问题吗?谢谢,是的,我只是不知道我在找什么!这回答了你的问题吗?谢谢,是的,我只是不知道我在找什么!