Pandas: Optimizing Some Python Code By Getting Rid Of DataFrame.apply()
the following code is produced using python 2.7 and pandas 0.9.1. I have a dataframe with two columns 'minor' and 'major'. I calculate the 'critical' value by taking the max absolu
Solution 1:
Random thoughts: to get the indices, you can use .idxmax
instead of max
, namely
>>> w = df[['minor','major']].abs().idxmax(axis=1)
>>> w
10 minor
20 major
30 minor
dtype: object
and then you could use lookup
(there's probably something simpler, but I'm missing it right now):
>>> df.lookup(df.index, w)
array([ -6. , 3. , 19.2])
IOW:
>>> df['critic_vector'] = df[['minor','major']].abs().idxmax(axis=1)
>>> df['critic'] = abs(df.lookup(df.index, df.critic_vector))
>>> df
major minor critic_vector critic
10 2.0 -6.0 minor 6.0
20 3.0 -2.3 major 3.0
30 7.4 19.2 minor 19.2
I'm not super-happy with the lookup
line -- you could replace it with your original max
call, of course -- but I think the idxmax
approach isn't a bad one.
Post a Comment for "Pandas: Optimizing Some Python Code By Getting Rid Of DataFrame.apply()"