Based on Sort pandas DataFrame with function over column values
I want to apply a function such as log()
to a data frame using the .assign()
method to create a temporary column and use it as a sorting criteria, however, I can't pass the axis parameter like the way it works for the .apply()
method.
Here's a sample code:
from numpy.random import randint
set.seed(0)
df = pd.DataFrame({'value':[randint(1,10) for i in range(0,10)], 'reading': [randint(1,10) for i in range(0,10)]})
value reading
0 8 6
1 5 9
2 3 7
3 8 2
4 6 1
5 4 9
6 6 2
7 3 5
8 2 2
9 8 8
I can't use .assign() method like this:
df.assign(log = log(df.value/df.reading))
raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>
or
df.assign(log = lambda x: log(x.value/x.reading))
raise TypeError("cannot convert the series to " "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>
But it works fine with .apply() method:
df.apply(lambda x: log(x.value/x.reading), axis=1)
0 0.287682
1 -0.587787
2 -0.847298
3 1.386294
4 1.791759
5 -0.810930
6 1.098612
7 -0.510826
8 0.000000
9 0.000000
dtype: float64
Any workaround to use assign or a different method to use it as a temporary column in sorting?