使用.between()检查长/纬度位置从字典中将其拉出时出错

时间:2019-04-25 14:12:35

标签: python pandas dictionary location

我正在浏览按特定ID分组的数据框,并尝试根据字典中的某些长/纬度位置返回位置表面类型。数据集的问题在于它以每秒100帧的速度创建,因此我试图找到中值,因为此之前和之后的值都不正确。

我正在使用熊猫jupyter笔记本,并且有

这是我要从字典中提取位置的功能。该位置只是一个虚构的例子

pitch_boundaries = {
    'Astro': {'max_long': -6.123456, 'min_long': -6.123456, 
'max_lat': 53.123456, 'min_lat': 53.123456},
}


def get_loc_name(loc_df, pitch_boundaries):

for pitch_name, coord_limits in pitch_boundaries.items():
    between_long_limits = loc_df['longitude'].median().between(coord_limits['min_long'], coord_limits['max_long'])
    between_lat_limits = loc_df['latitude'].median().between(coord_limits['min_lat'], coord_limits['max_lat'])
    if between_long_limits.any() and between_lat_limits.any():
        return pitch_name
# If we get here then there is no pitch.

在这里打电话

def makeAverageDataFrame(df):
    pitchBounds = get_loc_name(df, pitch_boundaries)
     i = len(df_average.index)
     df_average.loc[i] = [pitchBounds]

最后出现错误的地方

for region, df_region in df_Will.groupby('session_id'):
    makeAverageDataFrame(df_region)

实际结果
# AttributeError: 'float' object has no attribute 'between'
或者如果我删除了.median()None

我想要的是带有

之类的新数据框
|surface|

|Astro|

|Grass|

|Astro|

1 个答案:

答案 0 :(得分:0)

loc_df['longitude']是一个序列,而loc_df['longitude'].median()为您提供一个没有between方法的浮点数。尝试loc_df[['longitude']]

def get_loc_name(loc_df, pitch_boundaries):
    for pitch_name, coord_limits in pitch_boundaries.items():
        between_long_limits = loc_df[['longitude']].median().between(coord_limits['min_long'], coord_limits['max_long'])
        between_lat_limits = loc_df[['latitude']].median().between(coord_limits['min_lat'], coord_limits['max_lat'])
        if between_long_limits.any() and between_lat_limits.any():
            return pitch_name

返回None的问题是您的makeAverageDataFrame不返回任何内容(None)。试试:

def makeAverageDataFrame(df):
    pitchBounds = get_loc_name(df, pitch_boundaries)
    return pitchBounds