将具有分钟分秒(DMS)坐标的 pandas 数据帧转换为十进制度 [英] Converting pandas data frame with degree minute second (DMS) coordinates to decimal degrees

查看:112
本文介绍了将具有分钟分秒(DMS)坐标的 pandas 数据帧转换为十进制度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如下所示,并且想要转换 Latitude 度,分钟,秒格式的经度 列转换为十进制度-负数表示正确的半球。

I have a data frame like the below and would like to convert the Latitude and Longitude columns in Degree, Minute, Second format into decimal degrees - with negative for the correct hemisphere. Is there an easy way to do that?

Parent Company  CPO PKO Latitude    Longitude
Incasi Raya X       0°51'56.29"S    101°26'46.29"E
Incasi Raya X       1°23'39.29"S    101°35'30.45"E
Incasi Raya X       0°19'56.63"N    99°22'56.36"E
Incasi Raya X       0°21'45.91"N    99°37'59.68"E
Incasi Raya X       1°41'6.56"S 102°14'7.68"E
Incasi Raya X       1°15'2.13"S 101°34'30.38"E
Incasi Raya X       2°19'44.26"S    100°59'34.55"E
Musim Mas   X       1°44'55.94"N    101°22'15.94"E

例如 0°51'56.29 S 将转换为 -0.8656361

推荐答案

将我的答案基于SO的函数,您可以像这样:

Basing my answer on a function from SO you can do it like this:

有趣的是,对于带有+的数据集,该答案的速度也比MaxU和Amis答案快2倍500行。我敢打赌,瓶颈是str.extract(),但显然很奇怪。

Interestingly this answer is also 2x as fast as MaxU and Amis answer for a dataset with +500 rows. My bet is that the bottleneck is str.extract(). But something is clearly strange.

import pandas as pd
import re

#https://stackoverflow.com/questions/33997361
def dms2dd(s):
    # example: s = """0°51'56.29"S"""
    degrees, minutes, seconds, direction = re.split('[°\'"]+', s)
    dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60);
    if direction in ('S','W'):
        dd*= -1
    return dd

df = pd.DataFrame({'CPO': {0: 'Raya', 1: 'Raya'},
 'Latitude': {0: '0°51\'56.29"S', 1: '1°23\'39.29"S'},
 'Longitude': {0: '101°26\'46.29"E', 1: '101°35\'30.45"E'},
 'PKO': {0: 'X', 1: 'X'},
 'ParentCompany': {0: 'Incasi', 1: 'Incasi'}})

df['Latitude'] = df['Latitude'].apply(dms2dd)
df['Longitude'] = df['Longitude'].apply(dms2dd)

打印df返回:

    CPO   Latitude   Longitude PKO ParentCompany
0  Raya  -0.865636  101.446192   X        Incasi
1  Raya  -1.394247  101.591792   X        Incasi






更新:要纠正错误,您可以执行以下操作:


Update: To correct your mistake you could do something in the lines of:

m = df['Latitude'].str[-2] != '"'
df.loc[m, 'Latitude'] = df.loc[m, 'Latitude'].str[:-1] + '"' + df.loc[m, 'Latitude'].str[-1]

完整示例:

import re

s1 = """0°51'56.29"S"""
s2 = """0°51'56.29S"""

df = pd.Series((s1,s2)).to_frame(name='Latitude')

m = df['Latitude'].str[-2] != '"'
df.loc[m, 'Latitude'] = df.loc[m, 'Latitude'].str[:-1] + '"' + df.loc[m, 'Latitude'].str[-1]

print(df)

这篇关于将具有分钟分秒(DMS)坐标的 pandas 数据帧转换为十进制度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆