Using 'apply' in Pandas (externally defined function)

monkeybiz7

I have a Dataframe, table, that looks like this:

year name     prop     sex  soundex
1880 John     0.081541 boy  J500
1880 William  0.080511 boy  W450
....
2008 Elianna  0.000127 girl E450

I'm trying to group table by 'year', and access select indices from the column 'name' for each group.

My code is as follows (pretend that special_indices is already defined):

def get_indices_func(x):
    name = [x['name'].iloc[y] for y in special_indices]
    return pd.Series(name)


table.groupby(by='year').apply(get_indices_func)

I got the following error:

/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
    722         """
    723         try:
--> 724             return self._engine.get_value(series, key)
    725         except KeyError, e1:
    726             if len(self) > 0 and self.inferred_type == 'integer':

KeyError: 1000 

What am I doing wrong? I think I'm not really understanding how apply (and its cousins, aggregate and agg) works. If someone could explain, I'd be ever so grateful!

FooBar

An alternative solution:

df.groupby('year').apply(lambda x: x.sort('prop', ascending=False).iloc[0]['name'])

What is happening here?

First, as with Woody, we group by the correct column. apply() will deliver group-level data to that function. Instead, for understanding purposes, I could had written

define takeAGroupAndGiveBackMax(group):
    # year level data: first sort it by prop, descending
    group.sort('prop', ascending=False, inplace=True)
    # now return value 'name' of the first entry
    return group.iloc[0]['name']

# the following will give you a data set, indexed on whatever you grouped it by (here: year), and have a columns all the properties you return.    
df.groupby('year').apply(takeAGroupAndGiveBackMax)

In order to understand these, you should play around with the function. Try returning multiple columns, multiple rows, and you will see what apply() returns to you. It is really a powerful tool that pandas gives you here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a javascript function defined externally using react?

From Dev

Python pandas apply function defined in class

From Java

apply using a function with iloc pandas

From Dev

Is it possible to use an externally defined function in a closure

From Dev

pandas apply User defined function to grouped dataframe on multiple columns

From Dev

Using result_type with pandas apply function

From Dev

Using a shift() function within an apply function to compare rows in a Pandas Dataframe

From Dev

Apply function with args in pandas

From Dev

Alternative to apply function in pandas

From Dev

apply to the entire dataframe a user-defined function involving another dataframe in pandas

From Dev

Apply function to pandas dataframe row using values in other rows

From Dev

Dealing with None values when using Pandas Groupby and Apply with a Function

From Dev

Using rolling_apply with a function that requires 2 arguments in Pandas

From Dev

Python custom function using rolling_apply for pandas

From Dev

how to apply Functions on numpy arrays using pandas groupby function

From Dev

In Pandas, how to apply a customized function using Group mean on Groupby Object

From Dev

Performance of custom function while using .apply on Pandas Dataframes

From Dev

Pandas: The apply function I am using is giving me wrong results

From Dev

Performance of custom function while using .apply on Pandas Dataframes

From Dev

Use function (not using lambda) with apply method for pandas DataFrame

From Dev

Using pandas apply

From Dev

apply() in R with user-defined function

From Dev

Exception Handling in Pandas .apply() function

From Java

Apply ewm function on Pandas groupby

From Dev

Speeding up Pandas apply function

From Dev

Pandas Groupby Apply Function to Level

From Dev

pandas apply function with arguments no lambda

From Dev

Counting within Pandas apply() function

From Dev

Apply Customize Cumulative Function to Pandas

Related Related

  1. 1

    How to call a javascript function defined externally using react?

  2. 2

    Python pandas apply function defined in class

  3. 3

    apply using a function with iloc pandas

  4. 4

    Is it possible to use an externally defined function in a closure

  5. 5

    pandas apply User defined function to grouped dataframe on multiple columns

  6. 6

    Using result_type with pandas apply function

  7. 7

    Using a shift() function within an apply function to compare rows in a Pandas Dataframe

  8. 8

    Apply function with args in pandas

  9. 9

    Alternative to apply function in pandas

  10. 10

    apply to the entire dataframe a user-defined function involving another dataframe in pandas

  11. 11

    Apply function to pandas dataframe row using values in other rows

  12. 12

    Dealing with None values when using Pandas Groupby and Apply with a Function

  13. 13

    Using rolling_apply with a function that requires 2 arguments in Pandas

  14. 14

    Python custom function using rolling_apply for pandas

  15. 15

    how to apply Functions on numpy arrays using pandas groupby function

  16. 16

    In Pandas, how to apply a customized function using Group mean on Groupby Object

  17. 17

    Performance of custom function while using .apply on Pandas Dataframes

  18. 18

    Pandas: The apply function I am using is giving me wrong results

  19. 19

    Performance of custom function while using .apply on Pandas Dataframes

  20. 20

    Use function (not using lambda) with apply method for pandas DataFrame

  21. 21

    Using pandas apply

  22. 22

    apply() in R with user-defined function

  23. 23

    Exception Handling in Pandas .apply() function

  24. 24

    Apply ewm function on Pandas groupby

  25. 25

    Speeding up Pandas apply function

  26. 26

    Pandas Groupby Apply Function to Level

  27. 27

    pandas apply function with arguments no lambda

  28. 28

    Counting within Pandas apply() function

  29. 29

    Apply Customize Cumulative Function to Pandas

HotTag

Archive