How to efficiently apply a function to each DataFrame of a Pandas Panel

paco_uk

I am trying to apply a function to every DataFrame in a Pandas Panel. I can write it as a loop but the indexing seems to take a long time. I am hoping a builtin Pandas function might be faster.

I have data frames which look like (in reality about 50 rows per column):

mydata = pd.DataFrame( { 'hits' : [ 123, 456,678 ], 'sqerr' : [ 253, 641, 3480] } )

They are arranged in a panel with a multi-index key:

mydict = { (0, 20 ) : mydata, (30, 40 ) : moredata }
mypanel = pd.Panel( mydict )

The panel looks like this:

<class 'pandas.core.panel.Panel'>
Dimensions: 1600 (items) x 48 (major_axis) x 2 (minor_axis)
Items axis: (-4000, -4000) to (3800, 3800)
Major_axis axis: 0 to 47
Minor_axis axis: hits to sqerr

I have a function which takes a DataFrame and outputs a number:

def condenser( df ):
    return some_stuff( df['hits'], df['sqerr'] )

I want to reduce my Panel to a Series, indexed by my multi-index and with results of my condenser function as its values.

I can do:

intermediate = []
for k, df in mypanel.iteritems():
    intermediate.append( condenser( df ) )

result = pd.Series( results, index = pypanel.items )

which gives the required result, but when I profile it, only 4% of the time is spent in my condenser function. Most of the time is spent in iteritems and __getitem__ so I wondered if it could be done better.

I looked at mypanel.apply( condenser, axis = 'items' ) but this loops over each column of my DataFrames separately. Is there something which would apply a function to each DataFrame?

P.s. I am using Python 2.7.9 and pandas 0.15.2

user4270027

apply is correct, but the usage is:

mypanel.apply(condenser, axis=[1,2])

This will pass a 48 x 2 DataFrame into condenser.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function on each column in a pandas dataframe

From Dev

Apply function to each cell in DataFrame multithreadedly in pandas

From Dev

How to efficiently subtract each row from pandas dataframe?

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

From Dev

How to apply function to multiple pandas dataframe

From Java

Apply function to each cell in DataFrame

From Dev

Apply function to each cell in DataFrame

From Dev

apply a function to each row of the dataframe

From Dev

Pandas dataframe: how to apply describe() to each group and add to new columns?

From Dev

Python - Add a column to each dataframe of a Pandas Panel

From Dev

How to apply custom function to pandas data frame for each row

From Dev

How to apply function on a dataframe

From Dev

How to efficiently partially apply a function in R?

From Dev

How to apply a function to every value in a column in a pandas dataframe?

From Dev

Pandas: How to use applymap/apply function with arguements to a dataframe without looping

From Dev

Create entirely new dataframe efficiently from groupby .agg() or .apply() in Pandas?

From Dev

Use pandas DataFrame / Panel .apply() together with index/column information

From Dev

apply a function to each cell in a column of a dataframe in R

From Dev

Apply function to each row of Spark DataFrame

From Dev

Apply a function to each column in a dataframe in R

From Dev

Delist each row in a dataframe using apply function

From Dev

Apply function within each subset of a dataframe

From Dev

pandas: apply a function to each unique element in a dataframe column and merge back the output

From Dev

How to efficiently label each value to a bin after I created the bins by pandas.cut() function?

From Dev

How to replace efficiently values on a pandas DataFrame?

From Dev

How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

From Dev

How to efficiently calculate running maxima in a Pandas dataframe?

From Dev

How to efficiently change data layout of a DataFrame in pandas?

Related Related

  1. 1

    Apply function on each column in a pandas dataframe

  2. 2

    Apply function to each cell in DataFrame multithreadedly in pandas

  3. 3

    How to efficiently subtract each row from pandas dataframe?

  4. 4

    pandas DataFrame, how to apply function to a specific column?

  5. 5

    How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

  6. 6

    How to apply function to multiple pandas dataframe

  7. 7

    Apply function to each cell in DataFrame

  8. 8

    Apply function to each cell in DataFrame

  9. 9

    apply a function to each row of the dataframe

  10. 10

    Pandas dataframe: how to apply describe() to each group and add to new columns?

  11. 11

    Python - Add a column to each dataframe of a Pandas Panel

  12. 12

    How to apply custom function to pandas data frame for each row

  13. 13

    How to apply function on a dataframe

  14. 14

    How to efficiently partially apply a function in R?

  15. 15

    How to apply a function to every value in a column in a pandas dataframe?

  16. 16

    Pandas: How to use applymap/apply function with arguements to a dataframe without looping

  17. 17

    Create entirely new dataframe efficiently from groupby .agg() or .apply() in Pandas?

  18. 18

    Use pandas DataFrame / Panel .apply() together with index/column information

  19. 19

    apply a function to each cell in a column of a dataframe in R

  20. 20

    Apply function to each row of Spark DataFrame

  21. 21

    Apply a function to each column in a dataframe in R

  22. 22

    Delist each row in a dataframe using apply function

  23. 23

    Apply function within each subset of a dataframe

  24. 24

    pandas: apply a function to each unique element in a dataframe column and merge back the output

  25. 25

    How to efficiently label each value to a bin after I created the bins by pandas.cut() function?

  26. 26

    How to replace efficiently values on a pandas DataFrame?

  27. 27

    How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

  28. 28

    How to efficiently calculate running maxima in a Pandas dataframe?

  29. 29

    How to efficiently change data layout of a DataFrame in pandas?

HotTag

Archive