Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

First as you shown data does not contain any header, you should say it to read_csv:

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

Next as you want to apply the same scalar function to all the cells of your dataframe, a simple way is to tranform it column by column with pandas methods:

result = data.transform(lambda s: s.transform(deg))

From your example data, it gives

            0           1
0  303.100963  183.097133
1  183.858846  783.053217
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading