I've been using pandas apply method for both series and dataframe, but I am obviously still missing something, because I'm stumped on a simple function i'm trying to execute.
This is what I was doing:
def minmax(row):
return (row - row.min())/(row.max() - row.min())
row.apply(minmax)
but, this returns an all zero Series. For example, if
row = pd.Series([0, 1, 2])
then
minmax(row)
returns [0.0, 0.5, 1.0], as desired. But, row.apply(minmax) returns [0,0,0].
I believe this is because the series is of ints and the integer division returns 0. However, I don't understand,
- why it works with minmax(row) (shouldn't it act the same?), and
- how to cast it correctly in the apply function to return appropriate float values (i've tried to cast it using .astype and this gives me all NaNs... which I also don't understand)
- if apply this to a dataframe, as df.apply(minmax) it also works as desired. (edit added)
i suspect i'm missing something fundamental in how the apply works... or being dense. either way, thanks in advance.