Skip to content

Instantly share code, notes, and snippets.

/cos_sin.py
Created Oct 30, 2017

Embed
What would you like to do?
Input column name and max value in that column. Function converts values in to sin and cos components.
# Convert cyclical values from standard 0..N values in to radians
def degrees_2_rads(row):
radians = math.radians(row)
return radians
def rads_2_cos_val(row):
cos_val = math.cos(row)
return cos_val
def rads_2_sin_val(row):
sin_val = math.sin(row)
return sin_val
#Input column name and max value in that column. Function converts values in to sin and cos components
def calc_cyclical_coord(column, max_value):
for i in range(len(merged_dfs)):
data = merged_dfs[i]
# create new columns
new_column_name_c = column + "_c"
new_column_name_s = column + "_s"
data[new_column_name_c] = 0.0
merged_dfs[i][new_column_name_c] = 0.0
data[new_column_name_s] = 0.0
merged_dfs[i][new_column_name_s] = 0.0
#iterate through rows and calculate new values
try:
data[column] = data[column].apply(int)
except:
print('Exception Caught')
pass
data[column] *= (360/max_value)
data[new_column_name_c] = data[column].apply(degrees_2_rads)
data[new_column_name_c] = data[new_column_name_c].apply(rads_2_cos_val)
data[new_column_name_s] = data[column].apply(degrees_2_rads)
data[new_column_name_s] = data[new_column_name_s].apply(rads_2_sin_val)
del data[column]
merged_dfs[i][new_column_name_c] = data[new_column_name_c]
merged_dfs[i][new_column_name_s] = data[new_column_name_s]
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.