应用于日期框架的 pandas 产生"...的内置方法值" [英] Pandas apply to dateframe produces '<built-in method values of ...'

查看:71
本文介绍了应用于日期框架的 pandas 产生"...的内置方法值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建 GeoJSON对象.我的输入是一个csv,其中包含一个地址列,一个lat列和一个lon列.然后,我从坐标中创建了Shapely点,将它们缓冲到给定的半径之外,并通过映射选项获得了坐标字典-到目前为止,一切都很好.然后,在参考了这个问题之后,我写了以下函数可获取一系列词典:

def make_geojson(row): return {'geometry':row['geom'], 'properties':{'address':row['address']}}

I'm trying to build a GeoJSON object. My input is a csv with an address column, a lat column, and a lon column. I then created Shapely points out of the coordinates , buffer them out by a given radius, and get the dictionary of coordinates via the mapping option- so far, so good. Then, after referring to this question, I wrote the following function to get a Series of dictionaries:

def make_geojson(row): return {'geometry':row['geom'], 'properties':{'address':row['address']}}

然后我就这样应用了:

data['new_output'] = data.apply(make_geojson, axis=1)

我的结果列中充满了这些内容:<built-in method values of dict object at 0x10...

My resulting column is full of these: <built-in method values of dict object at 0x10...

最奇怪的部分是,当我直接调用函数时(即make_geojson(data.loc[0])实际上我确实得到了我期望的字典.也许更奇怪的是,当我调用从apply获取的函数时(例如data.output[0]()data.loc[0]['output']()),我得到以下列表的等效项: [data.loc[0]['geom'], {'address':data.loc[0]['address']}],即我要获取的字典的值(而不是键).

The weirdest part is, when I directly call the function (i.e. make_geojson(data.loc[0]) I do in fact get the dictionary I'm expecting. Perhaps even weirder is that, when I call the functions I'm getting from the apply (e.g. data.output[0](), data.loc[0]['output']()) I get the equivalent of the following list: [data.loc[0]['geom'], {'address':data.loc[0]['address']}], i.e. the values (but not the keys) of the dictionary I'm trying to get.

对于那些在家中玩耍的人来说,这是一个玩具示例:

For those of you playing along at home, here's a toy example:

from shapely.geometry import Point, mapping
import pandas as pd

def make_geojson(row):
    return {'geometry':row['geom'], 'properties':{'address':row['address']}}

data = pd.DataFrame([{'address':'BS', 'lat':34.017, 'lon':-117.959}, {'address':'BS2', 'lat':33.989, 'lon':-118.291}])
data['point'] = map(Point, zip(data['lon'], data['lat']))
data['buffer'] = data['point'].apply(lambda x: x.buffer(.1))
data['geom'] = data.buffer.apply(mapping)
data['output'] = data.apply(make_geojson, axis=1)

推荐答案

感谢DSM指出这一点.经验教训:熊猫不适用于任意Python对象

Thanks, DSM, for pointing that out. Lesson learned: pandas is not good for arbitrary Python objects

这就是我最后要做的事情:

So this is what I wound up doing:

temp = zip(list(data.geom), list(data.address))
output = map(lambda x: {'geometry': x[0], 'properties':{'address':x[1]}}, temp)

这篇关于应用于日期框架的 pandas 产生"...的内置方法值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆