Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate and apply a function over a single level of a DataFrame with MultiIndex?

Tags:

python

pandas

Thanks to the response to my initial question, I now have a multi-indexed DataFrame the way that I want it. Now that I have the data in the data structure, I'm trying to get it out and wonder if there is a better way to do this. My two problems are related, but may have separate "ideal" solutions:

Sample DataFrame (truncated)

Experiment           IWWGCW         IWWGDW       
Lead Time                24     48      24     48
2010-11-27 12:00:00   0.997  0.991   0.998  0.990
2010-11-28 12:00:00   0.998  0.987   0.997  0.990
2010-11-29 12:00:00   0.997  0.992   0.997  0.992
2010-11-30 12:00:00   0.997  0.987   0.997  0.987
2010-12-01 12:00:00   0.996  0.986   0.996  0.986

Iteration

I'd like to be able to loop over this DataFrame where the iteration would take me down only 1 index dimension, i.e. an iteritems behavior that would return [('IWWGCW', df['IWWGCW']), ('IWWGDW', df['IWWGDW'])] and yield 2 DataFrames with Lead Time columns. My brute-force solution is to use a wrapper routine that basically does [(key, df[key] for key in df.columns.levels[0]]. Is there a better way to do this?

Apply

I'd also like to do things like "subtract the IWWGDW entries from everybody else" to compute paired differences. I tried to do df.apply(lambda f: f - df['IWWGDW']) but get a KeyError: ('IWWGDW', 'occurred at index 2010-11-26 12:00:00') regardless of if I use axis=1 or axis=0. I've tried rebuilding a new DataFrame using the iteration workaround identified above, but I always worry when I brute-force things. Is there a more "pandasic" way to do this sort of computation?

like image 445
Tim Whitcomb Avatar asked Jun 15 '12 19:06

Tim Whitcomb


People also ask

How do you iterate over a DataFrame?

In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.

How do I iterate through a specific column in a DataFrame?

You can use the for loop to iterate over columns of a DataFrame. You can use multiple methods to iterate over a pandas DataFrame like iteritems() , getitem([]) , transpose(). iterrows() , enumerate() and NumPy. asarray() function.

How do you use MultiIndex in Pandas?

Creating a MultiIndex (hierarchical index) object A MultiIndex can be created from a list of arrays (using MultiIndex. from_arrays() ), an array of tuples (using MultiIndex. from_tuples() ), a crossed set of iterables (using MultiIndex. from_product() ), or a DataFrame (using MultiIndex.

Which function is used to iterate over all columns in the DataFrame?

iteritems(): Dataframe class provides a member function iteritems() which gives an iterator that can be utilized to iterate over all the columns of a data frame.


1 Answers

I would suggest using groupby for iteration:

In [25]: for exp, group in df.groupby(level=0, axis=1):
   ....:     print exp, group
   ....:     
IWWGCW Experiment           IWWGCW       
Lead Time                24     48
2010-11-27 12:00:00   0.997  0.991
2010-11-28 12:00:00   0.998  0.987
2010-11-29 12:00:00   0.997  0.992
2010-11-30 12:00:00   0.997  0.987
2010-12-01 12:00:00   0.996  0.986
IWWGDW Experiment           IWWGDW       
Lead Time                24     48
2010-11-27 12:00:00   0.998  0.990
2010-11-28 12:00:00   0.997  0.990
2010-11-29 12:00:00   0.997  0.992
2010-11-30 12:00:00   0.997  0.987
2010-12-01 12:00:00   0.996  0.986

However, I see that this doesn't drop the top level as you're looking for. Ideally you would be able to write something like:

df.groupby(level=0, axis=1).sub(df['IWWGCW'])

and have that do the pair-wise subtraction, but because df['IWWGCW'] drops the level, the column names don't line up. This works, though:

In [29]: df.groupby(level=0, axis=1).sub(df['IWWGCW'].values)
Out[29]: 
Experiment           IWWGCW      IWWGDW       
Lead Time                24  48      24     48
2010-11-27 12:00:00       0   0   0.001 -0.001
2010-11-28 12:00:00       0   0  -0.001  0.003
2010-11-29 12:00:00       0   0   0.000  0.000
2010-11-30 12:00:00       0   0   0.000  0.000
2010-12-01 12:00:00       0   0   0.000  0.000

I'll think a bit more about this.

like image 68
Wes McKinney Avatar answered Sep 18 '22 22:09

Wes McKinney