VGB
VGB

Reputation: 497

How to read Degree Minute Seconds (DMS) data directly from a .CSV file using pandas into a dataframe as strings?

How to read Degree Minute Seconds (DMS) data directly from a .CSV file using pandas into a dataframe as strings? For eg: If I have a csv file with a column of data that is in DMS format,How to read it to a dataframe as string for further calculations?

76° 17' 51.2399" E
77° 26' 30.8322" E
76° 51' 29.7812" E
75° 45' 41.3540" E
76° 17' 51.2399" E

input file sample :enter link description here

When I use pandas.read_csv('test.csv) #test.csv is the input file I am getting an error

Traceback (most recent call last):

  File "<ipython-input-90-2af7440e7795>", line 1, in <module>
    df = pd.read_csv('test.csv')

  File "C:\ProgramData\Anaconda3\envs\obspy\lib\site-packages\pandas\io\parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)

  File "C:\ProgramData\Anaconda3\envs\obspy\lib\site-packages\pandas\io\parsers.py", line 448, in _read
    parser = TextFileReader(fp_or_buf, **kwds)

  File "C:\ProgramData\Anaconda3\envs\obspy\lib\site-packages\pandas\io\parsers.py", line 880, in __init__
    self._make_engine(self.engine)

  File "C:\ProgramData\Anaconda3\envs\obspy\lib\site-packages\pandas\io\parsers.py", line 1114, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)

  File "C:\ProgramData\Anaconda3\envs\obspy\lib\site-packages\pandas\io\parsers.py", line 1891, in __init__
    self._reader = parsers.TextReader(src, **kwds)

  File "pandas\_libs\parsers.pyx", line 529, in pandas._libs.parsers.TextReader.__cinit__

  File "pandas\_libs\parsers.pyx", line 749, in pandas._libs.parsers.TextReader._get_header

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 2: invalid start byte

Upvotes: 0

Views: 370

Answers (2)

Artyom Akselrod
Artyom Akselrod

Reputation: 976

Try using different encoding:

import pandas as pd
df = pd.read_csv('test1.csv', encoding='windows-1252')

Upvotes: 1

NYC Coder
NYC Coder

Reputation: 7594

CSV file:

id,string
1,76° 17' 51.2399" E
2,77° 26' 30.8322" E
3,76° 51' 29.7812" E
4,75° 45' 41.3540" E
5,76° 17' 51.2399" E

Code:

df = pd.read_csv('test.csv')
print(df)

   id              string
0   1  76° 17' 51.2399" E
1   2  77° 26' 30.8322" E
2   3  76° 51' 29.7812" E
3   4  75° 45' 41.3540" E
4   5  76° 17' 51.2399" E

Can you share a sample of your CSV file?

Upvotes: 1

Related Questions