【发布时间】:2018-10-04 22:09:33
【问题描述】:
import pandas as pd
dict = {'Origin Region': [1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 5.0],
'Origin Latitude': [-36.45875, -36.24879, -36.789456, -38.14789, -36.15963, -36.159455, -36.2345, -36.12745],
'Origin Longitude': [145.14563, 145.15987, 145.87456, 146.75314, 145.75483, 145.78458, 145.123654, 145.11111]}
df = pd.DataFrame(dict)
centres_dict = {'Origin Region': [1.0, 2.0, 3.0, 4.0, 5.0],
'Origin Latitude': [-36.25361, -36.78541, -36.74859, -38.74123, -36.14538],
'Origin Longitude': [145.12345, 145.36241, 145.12365, 146.75314, 145.75483]}
centres_df = pd.DataFrame(centres_dict)
grouped_region = df.groupby('Origin Region')
for region, region_group in grouped_region:
outliers = region_group[['Origin Latitude', 'Origin Longitude']].where((region_group['Origin Latitude'] < -36.15))
outliers.dropna(inplace=True)
print(outliers)
if(~outliers.empty):
for index, outlier_value in outliers.iterrows():
for another_index, centre_value in centres_df.iterrows():
a = outlier_value['Origin Longitude']
b = outlier_value['Origin Latitude']
c = centres_df['Origin Longitude']
d = centres_df['Origin Latitude']
#find distance using the above and then find minimum distance
我正在尝试遍历数据帧 (df) 的每一组,然后根据某些条件过滤每组中的值,并在每个过滤后的值(异常值)与另一个数据帧中的所有值之间执行距离计算(中心_df)。
我有数据帧中的数据,我应该将它们转换为数组,然后使用 scipy cdist 计算距离吗?还是简单地使用循环并使用我自己的距离计算功能?我不确定这样做的最佳方法是什么。或者也许使用 apply 并调用我自己的距离函数?
【问题讨论】:
-
当您在
groupby循环中运行时,似乎没有单个 Outlier Dataframe。 -
我想计算 grouped_region 中每个组的异常值。以及这些组中每个异常值与中心数据框中所有点的距离。
-
请发布minimal reproducible example,包括示例数据、可编译代码以及所有需要的
import行和haversine()之类的赋值,我们可以在空的Python 环境中运行,以及所需的输出。另请参阅How to make good reproducible pandas examples。 -
嗨,Parafait,尽我所能提出最少且可重现的代码。
标签: python python-3.x pandas dataframe