病人通常绕开他们最近的医院去另一家医院做手术(原因很多)。我在英国有500,000名病人在24家医院就诊。
我想知道去医院就医的病人中有多大比例不是最近的选择。所以说,伦敦的一家医院有100名病人,20名应该去剑桥,他们的比例是20%。在下面的例子中,病人2最近的医院很可能是ulon1,ulat1。代表神经外科病房。
我有病人和医院的经度和纬度。因为保密,我不能显示病人密码的数据。
从本质上说,我的数据文件看起来像这样
d = {'patient_ID': [0, 1, 2, 3, 5,], 'patient_lon': [ 'plon1', 'plon2', 'plon3', 'plon4', 'plon5'], 'patient_lat': ['plat1','plat2', 'plat3', 'plat4','plat5'],\
'unit_lon' : ['ulon1', 'ulon2', 'ulon3', 'ulon4', 'ulon5'], 'unit_lat': ['ulat1', 'ulat2','ulat3', 'ulat4', 'ulat5']}
pd.DataFrame(data=d)|patient_ID |patient_lon | patient_lat | unit_lon | unit_lat
-------- ---------- ---------- -------- -------
|0 | plon1 | plat1 | ulon1 | ulat1
|1 | plon2 | plat2 | ulon2 | ulat2
|2 | plon3 | plat3 | ulon3 | ulat3
|3 | plon4 | plat4 | ulon4 | ulat4
|5 | plon5 | plat5 | ulon5 | ulat5我用Haversine方法计算了病人到他们所在医院的距离。
我如何用它来计算到24家医院的所有距离,并找出最小值为“本地”的距离。(它们都提供神经外科手术,这正是我感兴趣的)。然后将其与他们在一个新的dataframe列中实际使用的那个进行比较。
顺便说一句,我是外科医生,所以是这里的新手。
发布于 2022-09-26 08:56:12
下面的答案将数据稍作格式化,以便为某些列以元组形式存储lat长,希望这样做没有问题,但如果没有,请回复,我们将计算出答案。
1.模拟一些合理的病人位置。
from haversine import haversine
import pandas as pd
import numpy as np
import random
# number of simulated patient data
NUM_PATIENTS = 10
# a grid for sampling some patient locations from
COORD_LL = (51.578099, -0.232274)
COORD_UR = (52.797460, 1.556070)
GRID_LAT = np.linspace(COORD_LL[0], COORD_UR[0], num=NUM_PATIENTS)
GRID_LONG = np.linspace(COORD_LL[1], COORD_UR[1], num=NUM_PATIENTS)
GRID_LAT = np.around(GRID_LAT, decimals=4)
GRID_LONG = np.around(GRID_LONG, decimals=4)2.医院的贮存地点
接下来,我们将存储医院的名字和拉丁语系。在你上面的例子中,这将是你的24家英国医院,再一次,我在这里做了一些事情。
# names and locations of hospitals
HOSPITALS = dict(
ADDBR=(52.1779, 0.1464),
BURY=(52.2412, 0.6939),
PBOROUGH=(52.5548, -0.2613),
NWICH=(52.6091, 1.2609),
LONDON=(51.5553, -0.0993),
)3.装配数据
现在,我们使用上面的数据创建一些数据列表和数据。
# Simulate patient data: generate lists + dataframe
patient_latlongs = tuple(zip(GRID_LAT, GRID_LONG))
patient_id = [i for i in range(len(patient_latlongs))]
unit_visited = [random.choice(list(HOSPITALS.keys())) for x in range(len(patient_latlongs))]
unit_visited_latlong = [HOSPITALS.get(x) for x in unit_visited]
df = pd.DataFrame.from_dict(
{
"patient_ID": patient_id,
"patient_latlong": patient_latlongs,
"unit_visited": unit_visited,
"unit_visited_latlong": unit_visited_latlong,
}
)输出:
patient_ID patient_latlong unit_visited unit_visited_latlong
0 0 (51.5781, -0.2323) LONDON (51.5553, -0.0993)
1 1 (51.7136, -0.0336) PBOROUGH (52.5548, -0.2613)
2 2 (51.8491, 0.1651) ADDBR (52.1779, 0.1464)
3 3 (51.9846, 0.3638) LONDON (51.5553, -0.0993)
4 4 (52.12, 0.5625) BURY (52.2412, 0.6939)
5 5 (52.2555, 0.7613) PBOROUGH (52.5548, -0.2613)
6 6 (52.391, 0.96) LONDON (51.5553, -0.0993)
7 7 (52.5265, 1.1587) LONDON (51.5553, -0.0993)
8 8 (52.662, 1.3574) LONDON (51.5553, -0.0993)
9 9 (52.7975, 1.5561) BURY (52.2412, 0.6939)4.找到最近的医院
我们为找到最近的医院写了一个函数。这可能是为我们的例子定制了一点。如前所述,haversine是一个非常方便的库。此函数返回最近的医院的密钥。我们可以在我们的hospitals数据库中查找这个。
def find_nearest_hospital(latlong: tuple, hospitals: dict) -> str:
"""
Calculate nearest hospital and return name of it. Assumes a dict
storing hospital names as keys + latlong as tuples.
latlong: tuple
input latlong tuple
hospitals: dict
key / value pairs storing hospital names as keys and locations in latlong tuple values
returns:
name of closest hospital
"""
distances = {}
for hospital, location in hospitals.items():
distances.update({hospital: haversine(latlong, location)})
return min(distances, key=distances.get)5.计算距离
在dataframe中为病人分配新的列,计算最近的医院。转换比应用要快一些,理想情况下,我们可能会使用numpy矢量化函数,但对于您的用例来说,这可能足够快。如果没有,给我回信,我们可以看看。
df = df.assign(
closest_unit=df["patient_latlong"].transform(lambda x: find_nearest_hospital(x, HOSPITALS)),
closest_unit_lat=lambda x: x["closest_unit"].replace(
{k: v[0] for k, v in HOSPITALS.items()},
),
closest_unit_long=lambda x: x["closest_unit"].replace(
{k: v[1] for k, v in HOSPITALS.items()},
),
visited_closest=lambda x: (x["closest_unit"] == x["unit_visited"]),
)输出:
patient_ID patient_latlong unit_visited unit_visited_latlong \
0 0 (51.5781, -0.2323) PBOROUGH (52.5548, -0.2613)
1 1 (51.7136, -0.0336) BURY (52.2412, 0.6939)
2 2 (51.8491, 0.1651) ADDBR (52.1779, 0.1464)
3 3 (51.9846, 0.3638) NWICH (52.6091, 1.2609)
4 4 (52.12, 0.5625) ADDBR (52.1779, 0.1464)
5 5 (52.2555, 0.7613) ADDBR (52.1779, 0.1464)
6 6 (52.391, 0.96) NWICH (52.6091, 1.2609)
7 7 (52.5265, 1.1587) LONDON (51.5553, -0.0993)
8 8 (52.662, 1.3574) LONDON (51.5553, -0.0993)
9 9 (52.7975, 1.5561) PBOROUGH (52.5548, -0.2613)
closest_unit closest_unit_lat closest_unit_long visited_closest
0 LONDON 51.5553 -0.0993 False
1 LONDON 51.5553 -0.0993 False
2 ADDBR 52.1779 0.1464 True
3 ADDBR 52.1779 0.1464 False
4 BURY 52.2412 0.6939 False
5 BURY 52.2412 0.6939 False
6 BURY 52.2412 0.6939 False
7 NWICH 52.6091 1.2609 False
8 NWICH 52.6091 1.2609 False
9 NWICH 52.6091 1.2609 False在一个相关的/没有关联的笔记上,写在神经科病房里。
https://stackoverflow.com/questions/73843889
复制相似问题