如何在 pandas 数据帧中将方位角转换为方位角?

How to convert bearing to azimuth in pandas dataframe?

我想将一些方位角测量值转换为方位角,但我不知道如何将值(字符串 + 数字 + 字符)从我的列中分离出来。

import pandas
df = pd.DataFrame({'Bearing':["N47.00E","N48.50W","S67.00E"]})

我应该得到如下结果:

Azimuth
47
311.5
113

The calculation should be like this
N x E: Maintain the mid number
S x E: Azimuth = 180 - Bearing
S x W: Azimuth = 180 + Bearing
N x W: Azimuth = 360 - Bearing

有人能帮帮我吗?

首先,创建一个包含您的因素的数据框:

factors = {
    'NE': [1, 1],
    'SE': [180, -1],
    'SW': [180, 1],
    'NW': [360, -1],
}

factors = pd.DataFrame(factors).T

然后乘以它:

f = df['Bearing'].str[0] + df['Bearing'].str[-1]
df['Azimuth'] = factors.loc[f, 0].tolist() + (df['Bearing'].str.strip('NESW').astype(float) * factors.loc[f, 1].tolist())

输出:

>>> df
   Bearing  Azimuth
0  N47.00E     48.0
1  N48.50W    311.5
2  S67.00E    113.0