Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate a new column in Pandas based a on trignometric function?

Tags:

python

pandas

I have a dataframe with the following columns (sin and cos of a angle)

                SWD         CWD
2013-12-06  -0.394097   -0.350099
2013-12-07  -0.388295   -0.271105
2013-12-08  -0.391894   -0.202537
2013-12-09  -0.388662   -0.430063
2013-12-10  -0.396427   -0.433933

How can I create a new column with the arctan of the angle (atan(sin/cos)?

Thank you

Hugo

like image 341
Hugo Avatar asked Jun 24 '14 09:06

Hugo


People also ask

How do you calculate a new column based on the values of other columns in pandas Python?

Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.

How will you create a new column whose value is calculated from two other columns?

Create a new column by assigning the output to the DataFrame with a new column name in between the [] . Operations are element-wise, no need to loop over rows. Use rename with a dictionary or function to rename row labels or column names.


1 Answers

You can use numpy's arctan

In [42]:

df['ATAN'] = np.arctan(df['SWD']/df['CWD'])
df
Out[42]:
         Date       SWD       CWD      ATAN
0  2013-12-06 -0.394097 -0.350099  0.844451
1  2013-12-07 -0.388295 -0.271105  0.961284
2  2013-12-08 -0.391894 -0.202537  1.093787
3  2013-12-09 -0.388662 -0.430063  0.734874
4  2013-12-10 -0.396427 -0.433933  0.740260
like image 127
EdChum Avatar answered Nov 14 '22 21:11

EdChum