'How to convert numbers to other numbers with a function from a csv file

I have 2 column .csv file with contains numbers like:

20 12 24.2312, 12 12 23.312

12 15 26.123, 52 12 12.772

...

...

etc.

I want to convert these values with a function

data=pd.read_csv("test2.csv")

def deg(s): #takes H:M:S, returns S
  c = str(s).split()
  # 0: hour, 1: min, 2: sec
  v = float(c[0]) + float(c[1])/60 + float(c[2])/3600 #convert all to H
  return v*15 #convert to rad"""

for obj in range(len(data)):
    rss=(deg(obj[0]),deg(obj[1]))
    degres.append(rss)

However i get "'int' object is not subscriptable" error. How can i do what i want?



Solution 1:[1]

In your case obj is actually an integer: 0, 1, .... To get access to the numbers from the cells you have to do like this.

import pandas as pd

data=pd.read_csv("test2.csv", header = None)
print(data)

def deg(s): #takes H:M:S, returns S
  c = str(s).split()
  # 0: hour, 1: min, 2: sec
  v = float(c[0]) + float(c[1])/60 + float(c[2])/3600 #convert all to H
  return v*15 #convert to rad"""

degres = []

for i in range(len(data)):
    obj = data[i]
    rss=(deg(obj[0]),deg(obj[1]))
    degres.append(rss)

print(degres)

Most likely there are other ways to write it in a more pandas/dataframe style.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcel Preda