0

I have a data frame and I want to create a new column whose values are defined by values located in other columns (in the same row). It is very simple if I use simple operations (+, -, * and even abs). For example:

df['new_col'] = abs(df['col1']*df['col2'] - df['col3'])

Then I defined my own function in Python and tried to do the following:

df['new_col'] = my_func(df['col1'], df['col2'], df['col3'], df['col4']).

Unfortunately it did not work. I think the reason why it didn't work is because my_func contains asin, atan and other functions that cannot be applied to series. For example, if I try abs(df['col1']) I get no complains, but if I try asin(df['col1']) I get an error message:

TypeError: only length-1 arrays can be converted to Python scalars

Is there a trick that will let me use asin (or my own function my_func) in the same way abs or + are used?

4

1 回答 1

2

使用numpy通用功能

对于您的情况,请使用numpy.arcsin而不是math.asin.

于 2013-05-17T09:10:01.733 回答