[SOLVED] 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


I want to convert these values with a function


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)):

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


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

Answered By – Serge Ballesta

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *