【问题标题】:Create a dictionary with list of tuples - python创建一个包含元组列表的字典 - python
【发布时间】:2019-03-17 02:26:48
【问题描述】:

我有一个数据框,其中有一列包含存储为元组的坐标。

我需要通过解析元组对的每个值来创建一个字典并将其传递给字典。代码如下:

return {

    "data": [
            {
                "type": "scattermapbox",
                "lat": ,
                "lon": ,
                #"text": ,
                "mode": "markers",
                "marker": {
                "size": 3,
                "opacity": 1.0
            }
        }

      for i, row in Comp_data.iterrows() 

    ],
}

我试图找出一种方法来首先迭代列中的每个元组对并将其分配给字典中的值。

for i, row in Comp_data.iterrows():

    coords = row['HomeLocation']

    dict1 = {
                "type": "scattermapbox",
                "lat": coords[0],
                "lon": coords[1],
                "mode": "markers",

            }

【问题讨论】:

  • 怎么样:coords = row['HomeLocation']?元组在行中
  • 是的。这是个好主意。但是,元组中的第一个元素是纬度,另一个是经度,需要单独传递给字典。

标签: python pandas dictionary tuples


【解决方案1】:

我会稍微改变一下函数:

     x            hl            wl
0   da  (37.7, -121)  (47.7, -221)
1  wfh  (37.0, -125)  (47.0, -225)
2  rsh     (39, 135)     (49, 235)


def x(df):
    for i, row in df.iterrows():
        lat, lon = row['hl']
        yield  {
            "type": "scattermapbox",
            "lat":  lat,
            "lon":  lon,
            #"text": ,
            "mode": "markers",
            "marker": {
            "size": 3,
            "opacity": 1.0
        }
    }

print([s for s in x(df)])

[
  {
    "type": "scattermapbox",
    "lat": 37.7,
    "lon": -121,
    "mode": "markers",
    "marker": {
    "size": 3,
    "opacity": 1.0
   }
  },
  {
    "type": "scattermapbox",
    "lat": 37.0,
    "lon": -125,
    "mode": "markers",
    "marker": {
    "size": 3,
    "opacity": 1.0
  }
 },
 {
   "type": "scattermapbox",
   "lat": 39,
   "lon": 135,
   "mode": "markers",
   "marker": {
     "size": 3,
     "opacity": 1.0
     }
    }
  ]

或者在你的代码中:

 ...
 retvalue = []
 for i, row in df.iterrows():
    lat, lon = row['hl']
    retvalue.append({
            "type": "scattermapbox",
            "lat":  lat,
            "lon":  lon,
            #"text": ,
            "mode": "markers",
            "marker": {
            "size": 3,
            "opacity": 1.0
        })
 return retvalue

【讨论】:

    【解决方案2】:

    可能是:

    return {
        "data": [
                {
                    "type": "scattermapbox",
                    "lat": hl[0][0],
                    "lon": hl[0][1],
                    "mode": "markers",
                    "marker": {
                    "size": 3,
                    "opacity": 1.0
                }
            }
          for hl in Comp_data.loc[:, ['HomeLocation']].values
        ]
    }
    

    【讨论】:

      【解决方案3】:

      另一种方法是先将坐标分成单独的列。然后转换成字典列表。然后用不随行变化的其他属性更新每个字典。

      df = pd.DataFrame({
          "Transportation": ["Drive alone", "Work from home", "Ride share"], 
          "HomeLocation": [(37.7, -121), (37.0, -125),  (39, 135)],
          "WorkLocation": [(47.7, -221), (47.0, -225), (49, 235)]
      })
      data = df["HomeLocation"].apply(pd.Series).to_dict('records')
      other_properties = {
          "type": "scattermapbox",
          "mode": "markers",
          "marker": {
              "size": 3,
              "opacity": 1.0
          }
      }
      for dict in data:
          dict.update(other_properties)
      return {"data": data}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        • 1970-01-01
        • 2023-03-19
        • 2017-07-05
        • 2020-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多