Home > Software design >  How to convert bearing to azimuth in pandas dataframe?
How to convert bearing to azimuth in pandas dataframe?

Time:02-23

I would like to convert some bearing measurements to azimuth, but I can't figure out how I could do this and separate the values (strings numbers characters) from my column.

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

I should get results like:

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

Anyone could help me?

CodePudding user response:

First, create a dataframe containing your factors:

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

factors = pd.DataFrame(factors).T

Then multiply with it:

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())

Output:

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