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?
CodePudding user response:
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
CodePudding user response:
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.
