What are the methods of transforming geographic longitude and latitude data with Python
This article introduces the relevant knowledge of "what are the methods of converting geographic longitude and latitude data with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In mathematics, degrees, minutes and seconds that represent angles are represented by symbols such as °,', "and so on. The hexadecimal system is adopted between degrees and minutes, minutes and seconds, and their conversion relations are as follows:
1 °= 60'1 °= 3600 "1 °60"
Next, we use the data provided by the group to complete the operation of "degree, minute and second" data to "degree". The data screenshot is as follows.
When I got this requirement, I casually wrote down two solutions. But in the end, under the revision and improvement of the group friend Xiao Xiaoming (known as "Ming guy"), four solutions were provided.
① method 1: the apply () function import re of series
Import pandas as pd
Df = pd.read_csv ("t.txt", index_col=0)
Df.columns = ["latitude and longitude data"]
Def func (s):
Arr = re.findall ("\ d +", s)
Return int (arr [0]) + int (arr [1]) / 60+int (arr [2]) / 3600
Df ["final"] = df ["longitude and latitude data"] .apply (func)
Df
② method 2: split () method import re of str attribute in series
Import pandas as pd
Df = pd.read_csv ("t.txt", index_col=0)
Df.columns = ["latitude and longitude data"]
Tmp = df ["longitude and latitude data"] .str.split ("°|'|", expand=True) .values [:,: 3] .astype (int)
Df ["final"] = tmp [:, 0] + tmp [:, 1] / 60 + tmp [:, 2] / 3600
Df
③ method 3: extract () method import re of str attribute in series
Import pandas as pd
Df = pd.read_csv ("t.txt", index_col=0)
Df.columns = ["latitude and longitude data"]
Tmp = df ["longitude and latitude data"] .str.extract ("(\ d +) °(\ d +)'(\ d +)") .values.astype (int)
Df ["final"] = tmp [:, 0] + tmp [:, 1] / 60 + tmp [:, 2] / 3600
Df
④ method four: extractall () method import re of str attribute in series
Import pandas as pd
Df = pd.read_csv ("t.txt", index_col=0)
Df.columns = ["latitude and longitude data"]
Tmp = df ["longitude and latitude data"] .str.extractall ("(\ d +)") .unstack () .values.astype (int)
Df ["final"] = tmp [:, 0] + tmp [:, 1] / 60 + tmp [:, 2] / 3600
Df
This is the end of the content of "what are the methods of converting geographic longitude and latitude data with Python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!