使用元组列表创建字典-python

时间:2018-10-11 22:16:10

标签: python pandas dictionary tuples

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

enter image description here

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

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",

            }

3 个答案:

答案 0 :(得分:0)

我会稍微改变一下功能:

     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

答案 1 :(得分:0)

可能是:

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
    ]
}

答案 2 :(得分:0)

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

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}