Resample pandas dataframe only knowing the measurement result

I have a dataframe that looks like this:

Trial Measurement Data 0 0 12 1 4 2 12 1 0 12 1 12 2 0 12 1 12 2 NaN 3 12 

I want to convert my data so that each test has only two dimensions. Therefore, I want to turn it into something like this:

 Trial Measurement Data 0 0 8 1 8 1 0 12 1 12 2 0 12 1 12 

This rather unusual task is related to the fact that my data has intentional jitter on the side of the presentation of the stimulus.

I know that pandas has a resample function, but I don’t know how to apply it to my second level index, storing data in discrete categories based on the first level index: (

Also, I wanted to iterate over my first level indices, but apparently

 for sub_df in np.arange(len(df['Trial'].max())) 

It does not work, since 'Trial' is an index pandas cannot find it.

0
python pandas time-series resampling multi-index
Nov 20 '13 at 22:01
source share
1 answer

Well, it's not the prettiest I've ever seen, but from a frame similar to

 >>> df Trial Measurement Data 0 0 0 12 1 0 1 4 2 0 2 12 3 1 0 12 4 1 1 12 5 2 0 12 6 2 1 12 7 2 2 NaN 8 2 3 12 

then we can manually create two "medium" objects, and then use pd.melt to change the output:

 avg = df.groupby("Trial")["Data"].agg({0: lambda x: x.head((len(x)+1)//2).mean(), 1: lambda x: x.tail((len(x)+1)//2).mean()}) result = pd.melt(avg.reset_index(), "Trial", var_name="Measurement", value_name="Data") result = result.sort("Trial").set_index(["Trial", "Measurement"]) 

which produces

 >>> result Data Trial Measurement 0 0 8 1 8 1 0 12 1 12 2 0 12 1 12 
+1
Nov 20 '13 at 22:29
source share



All Articles